SQL WHERE, HAVING, AND, OR, AS CLAUSES | LEARN SQL

HEY GIRUILLIANS WELCOME BACK TO OUR SQL SERIES WE HAD POSTED 4 BLOGS ABOUT AND THIS IS 6TH IF YOU ARE NEW PLEASE READ PREVIOUS BLOGS TO LEARN WITH FLOW LET’S START

IN THE PREVIOUS CLASS WE HAD LEARNT ABOUT

1. ALTER COMMAND

2. SQL COPY TABLE

3. DISTINCT FUNCTION

4. TYPES OF SELECT FUNCTION


TODAY WE WILL LEARN ABOUT

1. SQL CLAUSES:

a. SQL WHERE

b. SQL AND

c. SQL OR

d. SQL AS SQL HAVING


a)SQL WHERE CLAUSE:- The WHERE clause is a SQL statement that filters the data retrieved from a table based on a specified condition. The following is how to write a where clause:
SELECT column1, column2, ...

FROM table_name WHERE condition;

In this syntax, column1, column2, ... refers to the columns that you want to retrieve data from, table_name is the name of the table from which you want to retrieve data, and the condition is the expression that specifies the filtering condition.

LET TABLE :- GUERILLATECK


Above command will show the name and class of std. with class 11.

Output


The "WHERE" clause uses some conditional selection
  • = equal
  • > greater than
  • < less than
  • >= greater than or equal
  • <= less than or equal
  • < > not equal to
b)SQL AND CLAUSE:- The SQL AND operator is used to combine multiple conditions in a WHERE clause. It allows you to specify multiple conditions that must all be true for a row to be returned. As an example, consider the syntax of the AND operator:

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

In this syntax, condition1, condition2, and condition3 are expressions that must all evaluate to true for a row to be returned. You can specify as many conditions as needed using the AND operator.

For example,


NOTE*-asterisk symbol(*) SHOW ALL THE COLUMNS OF TABLE 

ABOVE QUERY WILL SHOW THE ENTRY WHERE STREAMCHOSEN IS ARTS AND CLASS IS 11 BOTH CONDITIONS ARE TO BE SATISFIED

OUTPUT

c)SQL OR CLAUSE:- The SQL OR operator is used to combine multiple conditions in a WHERE clause. It allows you to specify multiple conditions, where at least one of the conditions must be true for a row to be returned. The syntax of the OR operator is as follows:

SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

In this syntax, condition1, condition2, and condition3 are expressions that must evaluate to true for a row to be returned. At least one of the conditions should be true for the OR operator to return the row.

FOR EXAMPLE:-


THE ABOVE QUERY WILL SHOW THE ENTRY WHERE STREAMCHOSEN IS ARTS OR CLASS IS 11 WHERE ONE OF THE CONDITIONS WILL SATISFY OR BOTH WILL SATISFY


d)SQL AS CLAUSE:- In SQL, the AS clause is used to assign an alias or a temporary name to a table or column in a query. The AS keyword is followed by the alias name that you want to assign. The syntax for using the AS clause is as follows:

SELECT column1 AS alias1, column2 AS alias2, ...
FROM table_name AS alias_name
WHERE condition;

In this syntax, column1 and column2 are the names of the columns that you want to select from table_name. alias1 and alias2 are the alias names that you want to assign to the selected columns. The AS keyword is used to assign the alias names. alias_name is the alias name that you want to assign to the table.

FOR EXAMPLE:-


THIS WILL TEMPORARILY CHANGE COL. NAME NAME TO STSNAME AND CLASS TO STD.

OUTPUT


e)SQL HAVING CLAUSE:- The HAVING clause was introduced in SQL to allow the filtering of query results based on aggregate functions and groupings, which cannot be achieved using the where clause that is used to filter individual rows.

SELECT column1, aggregate_function(column2)
FROM table_name
GROUP BY column1
HAVING condition;

We’ll be studying aggregate functions later.

Difference between HAVING and WHERE Clause

The most crucial query in an IT interview is the distinction between the database's WHERE and HAVING clauses.

The fundamental distinction between these two clauses can be seen in the following table, which compares them side by side. Whereas the HAVING clause utilizes condition to filter values from a group, the where clause employs condition to filter records before any groupings are established.







So that’s it for today guys if you want to learn more, so follow us...

Keep learning...

No comments:

Post a Comment