SQL Tutorial



WHERE CLAUSE IN SQL


Using the WHERE Clause to Filter Data in SQL

The WHERE clause in SQL is used to filter records that meet certain conditions. It allows you to retrieve only the data you're interested in by specifying the criteria.

By using the WHERE clause, you can refine your queries and make your results more relevant.

Syntax of the WHERE Clause

Here’s the basic syntax to filter records with the WHERE clause:

      SELECT column1, column2 FROM table_name WHERE condition;
    

In this structure, the condition could be any valid expression such as equality, comparisons, or logical operators.

Practical Example of WHERE Clause

Suppose we have a table called Employees with columns EmployeeID, Name, and Department.

      SELECT Name, Department FROM Employees WHERE Department = 'Sales';
    

This query will return the names and departments of all employees working in the 'Sales' department.

Using AND/OR with WHERE Clause

You can combine multiple conditions using the AND or OR logical operators to create more complex filters.

      SELECT Name, Age FROM Employees WHERE Department = 'Sales' AND Age > 30;
    

This will return employees who work in the 'Sales' department and are older than 30 years.

Tips for Using WHERE Clause

  • Use the AND operator to filter results by multiple criteria.
  • Use the OR operator when you need to fetch data that meets any of the conditions.
  • Make sure to use proper data types when writing your conditions (e.g., strings in quotes, numbers without quotes).
  • Be cautious of case sensitivity. SQL queries can be case-sensitive depending on the database system you're using.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review