100 Days of SQL

sql

Day 03 – SQL WHERE Clause

WHERE clause is used to filter data based on a specified condition or set of conditions. It is used in conjunction with the SELECT statement to retrieve only the rows from a table that meet the specified criteria. The basic syntax of a SELECT statement with a WHERE clause is:

SELECT column1, column2, ...
FROM table_name
WHERE condition;

In this syntax, column1, column2, etc. are the names of the columns that you want to retrieve data from, and table_name is the name of the table that you want to retrieve data from. The condition is a logical expression that evaluates to true or false for each row in the table.

Here’s an example of a SELECT statement with a WHERE clause that retrieves all the rows from a table named “customers” where the “country” column is equal to ‘USA’:

SELECT * FROM customers
WHERE country = 'USA';

In this example, the WHERE clause is used to filter the data and retrieve only the rows where the “country” column is equal to ‘USA’.

You can also use the WHERE clause with other logical operators such as < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), <> (not equal to), AND, OR, and NOT. Here’s an example:

SELECT * FROM orders
WHERE customer_id = 1234 AND order_date >= '2022-01-01';

In this example, the WHERE clause is used to retrieve only the rows from the “orders” table where the “customer_id” column is equal to 1234 AND the “order_date” column is greater than or equal to January 1, 2022.

The WHERE clause is a powerful tool for filtering and selecting data from a table based on specific criteria. It allows you to retrieve only the data that is relevant to your analysis or processing, making it easier to work with large amounts of data.