Categories: DBMS

SQL: WHERE clause

When retrieving data from a single table or by joining data from multiple tables, a condition is specified using the SQL WHERE clause. If the given condition is satisfied, then only it returns a specific value from the table. The WHERE clause should be used to filter the records and only retrieve those that are required.

Syntax:

SELECT column_names
FROM table_name
WHERE conditions;

The SELECT clause specifies the columns to be returned from the table(s), the WHERE clause contains the conditions that must evaluate to true for a row to be returned as a result.

Operators that can be used with where clause:
OperatorDescription
>Greater Than
>=Greater than or Equal to
<Less Than
<=Less than or Equal to
=Equal to
<>Not Equal to
BETWEENIn an inclusive Range
LIKESearch for a pattern
INTo specify multiple possible values for a column
Example:

Consider the PRODUCTS table having the following records −

IDITEMQUANTITYPRICE_PER_ITEM
1Chair67850.00
2Ball12600.00
3Jars9500.00
  • Query to select an item based on a condition:
SELECT ID, 
    ITEM, 
    QUANTITY, 
    PRICE_PER_ITEM 
    FROM PRODUCTS WHERE ID = 3;

Output:

IDITEMQUANTITYPRICE_PER_ITEM
3Jars9500.00
  • Query to fetch ITEM and QUANTITY of PRODUCTS with PRICE greater than 500.00
SELECT ID, 
    ITEM, 
    QUANTITY
    FROM PRODUCTS WHERE PRICE_PER_ITEM > 500.00;

Output:

IDITEMQUANTITY
1Chair67
2Ball12
  • Query to fetch PRICE for chair:
SELECT ID,ITEM,PRICE_PER_ITEM
    FROM PRODUCTS WHERE ITEM = "Chair";

Output:

IDITEMPRICE_PER_ITEM
1Chair850.00

Note: multiple conditions in the where clause can be used as per the requirements.

Note: also read about DCL Commands: GRANT and REVOKE

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago