BEST SQL WHERE Clause

The SQL WHERE Clause

The SQL WHERE clause is used to filter the records and extract only those records that fulfill a specific condition with the SELECT statement, but it can also be used with other SQL commands like INSERT, UPDATE, DELETE, and more.

SQL WHERE Clause Syntex:

SQL> SELECT column1, column2, column3 FROM table_name
WHERE condition;

SELECT: This keyword indicates that you want to retrieve data.

column1,column2,column3: These are the names of the columns you want to retrieve data from the table. You can specify one or more columns, separated by commas, or use * to select all columns.

FROM: This keyword specifies the table from which you want to retrieve data.

table_name: This is the name of the table where the data is stored.

Where: This keyword is followed by a condition that specifies which rows should be included in the result. The condition is evaluated for each row in the table.

condition: This is the condition that determines whether a row should be included in the result set. It’s typically expressed using comparison operators like =, <, >, <=, >=, and logical operators like AND, OR, and NOT.

Example of a SQL WHERE Clause:

Let’s say we have a table called students with the following data:

student_id first_name last_name age
1 John Verma 18
2 Jane Sharma 20
3 Alice Johnson 22
4 Bob Pandit 19

If you want to retrieve a list of students who are older than 20 years old, you can use the WHERE clause like this:

SQL> SELECT first_name, last_name FROM students
WHERE age > 20;

The result of this query would be:

first_name last_name
Alice Johnson

SQL WHERE Clause Operators Examples

OperatorDescription
=Equal
Greater than
Less than
>=Greater than or equal
<=Less than or equal
<> Not equal

In the above example,

We specified the columns first_name and last_name from which we wanted to retrieve the data.

We specified the students table from which we want to retrieve data.

The WHERE clause filters the data based on the condition age > 20, ensuring that only rows where the age is greater than 20 are included in the result.

So, the WHERE clause is essential for narrowing down the data you retrieve from a table, allowing you to focus on specific rows that meet your criteria.

Leave a Comment