Retrieve Data from table
To modify existing data of table, “Select” query is used,
let consider the table structure, Table name: “student”, Colums: Rollno, Name, Class, Subject, Pr, Th
Query Clauses
Example-1
Example-2
Example-3
Clause Purpose
WHERE Filters rows based on condition
ORDER BY Sorts result set
GROUP BY Groups rows for aggregation
HAVING Filters aggregated groups
IN Matches values from a list
BETWEEN Filters values within a range
LIKE Pattern matching
IS NULL Tests for NULL values
1. WHERE Clause
• Used to filter records that meet a specific condition.
• Applies to SELECT, UPDATE, DELETE statements.
SQL
SELECT * FROM students WHERE marks > 80;
SQL: for multiple condition use AND / OR
SELECT * FROM students WHERE marks > 80 and class=‘XI’;
#above query display all the rows where both the conditions are true
SELECT * FROM students WHERE marks > 80 or class=‘XI’;
#above query display all the rows where either of the conditions are true, like where marks are more then 80, or class is xi
or both. This may also include rows where marks are more then 80, but not in class xi and also where class is xi but marks
are not more then 80
2. ORDER BY Clause
• Sorts the result set in ascending (ASC) or descending (DESC) order.
• Can be used on one or more columns.
SQL: display record in descending order
SELECT name, marks FROM students ORDER BY marks DESC;
SQL: display record in ascending order
SELECT name, marks FROM students ORDER BY marks asc;
#By default it is ascending
SELECT name, marks FROM students ORDER BY marks;
3. GROUP BY Clause
• Groups rows that have the same values in specified columns.
• Often used with aggregate functions (SUM, COUNT, etc.).
SQL
SELECT class, COUNT(*) FROM students GROUP BY class;
4. HAVING Clause
• Used to filter groups created by GROUP BY.
• Similar to WHERE but applies to groups, not individual rows.
SQL
SELECT class, AVG(marks) FROM students GROUP BY class HAVING AVG(marks) > 75;
5. IN Clause
• Used to match a column’s value against a list of values.
SQL
SELECT * FROM students WHERE class IN ('10A', '10B');
6. BETWEEN Clause
• Filters records within a given range (inclusive).
SQL
SELECT * FROM students WHERE marks BETWEEN 60 AND 80;
7. LIKE Clause
• Used for pattern matching using % (any characters) and _ (single character).
SQL: Name starts with “A”
SELECT * FROM students WHERE name LIKE 'A%';
SQL: Name ends with “A”
SELECT * FROM students WHERE name LIKE ‘%A';
SQL: Name including character “A”
SELECT * FROM students WHERE name LIKE ‘%A%';
SQL: Name having second character a “A”
SELECT * FROM students WHERE name LIKE ‘_A%';
SQL: Name ends with third last character “A”
SELECT * FROM students WHERE name LIKE ‘%A_ _';
8. IS NULL / IS NOT NULL
Used to find records with NULL (or non-NULL) values.
SQL: the column is null
SELECT * FROM students WHERE email IS NULL;
SQL: the column is not null
SELECT * FROM students WHERE email IS NOT NULL;
These work on a set of rows and return a single result.
Function Description Example
SUM() Returns total sum SUM(salary)
AVG() Returns average value AVG(marks)
MAX() Returns highest value MAX(price)
MIN() Returns lowest value MIN(age)
COUNT() Returns number of rows COUNT(*) or COUNT(name)