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…

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Introduces 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

  1. Write a SQL query to fetch records of employees with EmpAge less than 35.
  2. 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.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

XML Sitemap Generator

Generate XML sitemaps for search engines.

Use tool

Timestamp Converter

Convert timestamps to human-readable dates.

Use tool

PDF Password Protector

Add or remove passwords from PDF files.

Use tool

Open Graph Preview Tool

Preview and test Open Graph meta tags for social media.

Use tool

Date Difference Calculator

Calculate days between two dates.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI 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 article

AI 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 article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help