SQL / SQL Basics
Using WHERE to Filter Data
The WHERE clause in SQL is a powerful tool for filtering data based on specific conditions. This tutorial will teach you how to use WHERE to enhance your data retrieval capabiliti…
Section overview
5 resourcesIntroduces the fundamentals of SQL, including basic syntax, queries, and data retrieval.
Introduction
In this tutorial, we aim at helping you understand how to use the SQL WHERE clause to filter data based on specific conditions. The WHERE clause is a powerful tool that can significantly enhance your data retrieval capabilities in SQL.
By the end of this tutorial, you will be able to:
- Understand the concept of the WHERE clause in SQL
- Apply the WHERE clause in SQL queries to filter data
- Use logical operators with the WHERE clause
Prerequisites: Basic knowledge of SQL and its syntax.
Step-by-Step Guide
The WHERE clause is used in SQL to filter records. It is used to extract only those records that fulfill a specified condition.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
You can use logical operators like =, <>, >, <, >=, <= along with WHERE clause to filter the results. Let's understand this with examples.
Code Examples
Consider a table called Employees with the following data:
| EmpId | EmpName | EmpAge | EmpSalary |
|---|---|---|---|
| 1 | John | 35 | 5000 |
| 2 | Smith | 40 | 6000 |
| 3 | David | 30 | 7000 |
| 4 | Sara | 20 | 8000 |
Example 1: Using WHERE with = operator
If you want to fetch records of the employee with EmpId = 1, the SQL query would look like this:
SELECT *
FROM Employees
WHERE EmpId = 1;
This will return the following output:
| EmpId | EmpName | EmpAge | EmpSalary |
|---|---|---|---|
| 1 | John | 35 | 5000 |
Example 2: Using WHERE with > operator
If you want to fetch records of employees with EmpSalary more than 6000, the SQL query would look like this:
SELECT *
FROM Employees
WHERE EmpSalary > 6000;
This will return the following output:
| EmpId | EmpName | EmpAge | EmpSalary |
|---|---|---|---|
| 3 | David | 30 | 7000 |
| 4 | Sara | 20 | 8000 |
Summary
In this tutorial, we learned about the SQL WHERE clause, its purpose, and how to use it with different operators to filter data. Continue practicing with different operators and conditions to grasp the concept better.
Practice Exercises
- Write a SQL query to fetch records of employees with EmpAge less than 35.
- Write a SQL query to fetch records of employees with EmpName not equal to 'John'.
Solutions:
SELECT *
FROM Employees
WHERE EmpAge < 35;
SELECT *
FROM Employees
WHERE EmpName <> 'John';
Keep experimenting with different conditions and operators to gain more confidence and proficiency with the WHERE clause.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article