You're offline — cached pages and worlds still work
Drishti Innovations logo
Drishti Innovations

SQL Queries & Joins

Select statements, aggregate functions, and multi-table joins.

What you'll learn

  • Writing SELECT queries with filtering, sorting, and grouping
  • Using aggregate functions (COUNT, SUM, AVG, MIN, MAX)
  • The difference between INNER, LEFT, RIGHT, and FULL JOINs
  • Writing subqueries and nested SELECT statements
  • Using DISTINCT to eliminate duplicate results

Key concepts

Basic SELECT Syntax

SELECT column1, column2
FROM table_name
WHERE condition
ORDER BY column1 ASC|DESC;

Clauses and Their Roles

ClausePurposeExample
SELECTChoose which columns to returnSELECT name, age
FROMSpecify the source tableFROM students
WHEREFilter rows before groupingWHERE age > 16
GROUP BYGroup rows that share a column valueGROUP BY class
HAVINGFilter groups after aggregationHAVING COUNT(*) > 5
ORDER BYSort results (ASC default, DESC optional)ORDER BY marks DESC
DISTINCTRemove duplicate rows from outputSELECT DISTINCT city

WHERE vs HAVING: WHERE filters individual rows; HAVING filters aggregated groups.

Aggregate Functions

FunctionReturns
COUNT(*)Number of rows
SUM(col)Total of numeric column
AVG(col)Mean value
MIN(col)Smallest value
MAX(col)Largest value

Example:

SELECT class, COUNT(*) AS total_students, AVG(marks) AS avg_marks
FROM students
GROUP BY class
HAVING AVG(marks) > 60
ORDER BY avg_marks DESC;

JOINs Explained

A JOIN combines rows from two tables based on a related column.

JOIN TypeReturns
INNER JOINOnly rows with matching values in both tables
LEFT JOINAll rows from the left table; NULL for non-matches on the right
RIGHT JOINAll rows from the right table; NULL for non-matches on the left
FULL JOINAll rows from both tables; NULL where no match exists

Sample Tables:

Students

student_idnamedept_id
1Anika10
2Rohan20
3PriyaNULL

Departments

dept_iddept_name
10Science
20Commerce
30Arts

INNER JOIN (only matched rows):

SELECT name, dept_name
FROM students
INNER JOIN departments ON students.dept_id = departments.dept_id;
-- Returns: Anika/Science, Rohan/Commerce

LEFT JOIN (all students, even without a department):

SELECT name, dept_name
FROM students
LEFT JOIN departments ON students.dept_id = departments.dept_id;
-- Returns: Anika/Science, Rohan/Commerce, Priya/NULL

RIGHT JOIN (all departments, even without students):

-- Returns: Anika/Science, Rohan/Commerce, NULL/Arts

FULL JOIN (everyone, matched or not):

-- Returns: Anika/Science, Rohan/Commerce, Priya/NULL, NULL/Arts

Subqueries

A subquery is a SELECT statement nested inside another query.

-- Find students who scored above the class average
SELECT name, marks
FROM students
WHERE marks > (SELECT AVG(marks) FROM students);

Subqueries can appear in SELECT, FROM, or WHERE clauses.

DISTINCT

SELECT DISTINCT city FROM students;
-- Returns each city only once, even if many students share it

Quick check

  1. Write a query to find the name and marks of the top 3 students from a table called results, sorted by marks in descending order.

  2. What is the difference between WHERE and HAVING? Give one example where HAVING is necessary and WHERE cannot be used instead.

  3. A teachers table and a courses table share a teacher_id column. Write an INNER JOIN query to display each teacher's name alongside their course name.

  4. Using the students table with columns (id, name, class, marks), write a query to display the average marks per class, but only for classes where the average exceeds 70.

  5. What does a LEFT JOIN return that an INNER JOIN does not? Describe a real-world scenario where a LEFT JOIN is the correct choice.

Key Takeaways (TL;DR)

  • What you'll learn
  • Key concepts
  • Quick check

Master this topic with Drishti OS

Get unlimited mock tests, AI-powered mentorship, and complete video courses when you join.

Start Free Practice