WHERE Clause in SQL

For Complete YouTube Video: Click Here

In this class, we will understand the WHERE Clause in SQL.

We have already discussed the concepts of the SELECT command.

WHERE Clause in SQL

In our discussion on the SELECT command, we have seen how to divide table columns vertically.

The WHERE clause is used to filter records.

It is used to extract only those records that fulfill a specified condition.

The WHERE clause is used in SELECT, UPDATE, DELETE, etc.!

To understand, we will use the table as shown below.

The syntax for the select query with where clause is as shown below.

SELECT column1, column2, … FROM table_name WHERE condition;

The WHERE clause is used after the table name.

After the WHERE clause, we have to provide the condition.

Based on the condition, the SQL will filter the rows from the table.

How will the SELECT command get executed?

To understand this, let us consider the query as shown below.

WHERE Clause in SQL 1
WHERE Clause in SQL 1

SELECT name, salary FROM students WHERE sex = ‘M’;

The above query is applied to the student’s table.

The condition after the where clause will be applied row by row.

First, the condition sex = ‘M’ is applied on the first row.

If the condition is true, that means if the sex of the first student is M, then that row will get selected.

In our case, it is true.

Similarly, the condition is applied to the next row and so on.

After applying the condition on all the rows, three will get selected as the condition sex = M is getting true.

Now the table has been horizontally divided.

The columns name and salary columns of the student’s table from the three rows will get selected.

The final output of the above query is as shown below.

WHERE Clause in SQL 2
WHERE Clause in SQL 2