Docsity

Essential sql topics with examples,queries and interview questions., Exams of Computer science

Toggle sidebar
bg1
SQL Made Simple
A clean beginner-friendly guide with explanations, examples, queries and interview practice
30 Essential SQL Topics โ€ข Practical Examples โ€ข Placement & Data Analyst Preparation
How to use this guide
Read one topic, study the example, thenchange the example yourself. SQL becomes mucheasier when you
practice small queries instead of memorizing de๏ฌnitions.
Sample tables used throughout the guide
employees
departments
employee_id | name | department_id | salary
department_id | department_na me
1 | Ravi | 10 | 50000
10 | IT
2 | Anu | 20 | 60000
20 | HR
3 | Kiran | 10 | 70000
30 | Sales
4 | Meena | 30 | 45000
Note: SQL synta x can vary slightly between MySQL, PostgreSQL, SQL Server and Oracle.
1. SELECT โ€” Reading Data
SELECT is used to retrieve data from a table.
Example query
SELECT nam e, salary
FROM employees;
What happens?
This returns the name andsalary of every employee.
Example result / note
nam e | salary
Ravi | 50000
Anu | 60000
Kiran | 70 000
Meena | 45000
2. WHERE โ€” Filtering Rows
WHEREreturns only rows that satisfy a condition.
Example query
SELECT nam e, salary
FROM employees
WHERE sal ary > 55000;
What happens?
Only employees earningmore than 55,000 are returned.
Example result / note
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Essential sql topics with examples,queries and interview questions. and more Exams Computer science in PDF only on Docsity!

SQL Made Simple

A clean beginner-friendly guide with explanations, examples, queries and interview practice

30 Essential SQL Topics โ€ข Practical Examples โ€ข Placement & Data Analyst Preparation

How to use this guide

Read one topic, study the example, then change the example yourself. SQL becomes much easier when you

practice small queries instead of memorizing definitions.

Sample tables used throughout the guide employees departments

employee_id | name | department_id | salary department_id | department_name

1 | Ravi | 10 | 50000 10 | IT

2 | Anu | 20 | 60000 20 | HR

3 | Kiran | 10 | 70000 30 | Sales

4 | Meena | 30 | 45000 Note: SQL syntax can vary slightly between MySQL, PostgreSQL, SQL Server and Oracle.

1. SELECT โ€” Reading Data

SELECT is used to retrieve data from a table.

Example query SELECT name, salary FROM employees;

What happens?

This returns the name and salary of every employee.

Example result / note name | salary Ravi | 50000 Anu | 60000 Kiran | 70000 Meena | 45000

2. WHERE โ€” Filtering Rows

WHERE returns only rows that satisfy a condition.

Example query SELECT name, salary FROM employees WHERE salary > 55000;

What happens?

Only employees earning more than 55,000 are returned.

Example result / note

Anu | 60000 Kiran | 70000

  1. DISTINCT โ€” Removing Duplicates

DISTINCT removes duplicate values from the result.

Example query SELECT DISTINCT department_id FROM employees;

What happens?

The result contains each department ID only once.

Example result / note 10 20 30

  1. ORDER BY โ€” Sorting Results

ORDER BY sorts the result. ASC means ascending and DESC means descending.

Example query SELECT name, salary FROM employees ORDER BY salary DESC;

What happens?

Employees are displayed from the highest salary to the lowest.

Example result / note Kiran | 70000 Anu | 60000 Ravi | 50000 Meena | 45000

  1. LIKE โ€” Pattern Matching

LIKE is used when you want to search for a pattern in text. % means any number of characters.

Example query SELECT name FROM employees WHERE name LIKE 'K%';

What happens?

This finds names beginning with K.

Example result / note

Kiran

  1. IN โ€” Matching Several Values

COUNT(*) counts every row.

Example result / note employee_count = 4

  1. SUM, AVG, MIN and MAX

These aggregate functions summarize numeric data.

Example query SELECT SUM(salary) AS total, AVG(salary) AS average, MIN(salary) AS lowest, MAX(salary) AS highest FROM employees;

What happens?

The query calculates four useful salary statistics.

Example result / note total = 225000 average = 56250 lowest = 45000 highest = 70000

  1. GROUP BY โ€” Creating Groups

GROUP BY groups rows so aggregate functions can calculate one result for each group.

Example query SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id;

What happens?

Each department gets its own average salary.

Example result / note 10 | 60000 20 | 60000 30 | 45000

  1. HAVING โ€” Filtering Groups

HAVING filters groups after GROUP BY. WHERE filters individual rows.

Example query SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id HAVING AVG(salary) > 50000;

What happens?

Only departments whose average salary is above 50,000 remain.

Example result / note 10 | 60000

  1. INNER JOIN โ€” Matching Two Tables

INNER JOIN returns rows where the join condition matches in both tables.

Example query SELECT e.name, d.department_name FROM employees e INNER JOIN departments d ON e.department_id = d.department_id;

What happens?

The employee table is connected to the department table using department_id.

Example result / note Ravi | IT Anu | HR Kiran | IT Meena | Sales

  1. LEFT JOIN โ€” Keep All Left Rows

LEFT JOIN keeps every row from the left table, even when there is no matching row on the right.

Example query SELECT d.department_name, e.name FROM departments d LEFT JOIN employees e ON d.department_id = e.department_id;

What happens?

Every department appears. If a department has no employee, the employee columns become NULL.

Example result / note IT | Ravi IT | Kiran HR | Anu Sales | Meena

  1. SELF JOIN โ€” Joining a Table to Itself

A self join is useful when rows in the same table are related, such as employees and their managers.

Example query SELECT e.name AS employee, m.name AS manager FROM employees e JOIN employees m ON e.manager_id = m.employee_id;

What happens?

Here the same employees table is used twice: once for the employee and once for the manager. The sample table

does not include manager_id, so this is a pattern example.

The CTE first creates high_paid. The outer query then reads from it.

Example result / note Anu | 60000 Kiran | 70000

  1. CASE โ€” Conditional Logic

CASE works like IF/ELSE logic inside SQL.

Example query SELECT name, salary, CASE WHEN salary >= 60000 THEN 'High' WHEN salary >= 50000 THEN 'Medium' ELSE 'Low' END AS salary_level FROM employees;

What happens?

Each employee is assigned a salary category.

Example result / note Ravi | 50000 | Medium Anu | 60000 | High Kiran | 70000 | High Meena | 45000 | Low

  1. Window Functions โ€” Ranking Without Grouping

Window functions calculate across related rows while keeping individual rows visible.

Example query SELECT name, department_id, salary, RANK() OVER ( PARTITION BY department_id ORDER BY salary DESC ) AS salary_rank FROM employees;

What happens?

Employees are ranked by salary within each department.

Example result / note IT: Kiran 1, Ravi 2 HR: Anu 1 Sales: Meena 1

  1. ROW_NUMBER โ€” Unique Row Numbers

ROW_NUMBER() gives each row a sequential number within a window.

Example query SELECT name, salary, ROW_NUMBER() OVER (ORDER BY salary DESC) AS row_no FROM employees;

What happens?

The highest-paid employee receives row number 1, the next receives 2, and so on.

Example result / note

This is a concept example; adapt the columns to your own table.

  1. RANK vs DENSE_RANK

RANK leaves gaps after ties; DENSE_RANK does not.

Example query SELECT name, salary, RANK() OVER (ORDER BY salary DESC) AS rnk, DENSE_RANK() OVER (ORDER BY salary DESC) AS dense_rnk FROM employees;

What happens?

If two employees tie for rank 1, RANK gives the next employee rank 3, while DENSE_RANK gives rank 2.

Example result / note

This is a concept example; adapt the columns to your own table.

  1. INSERT โ€” Adding Data

INSERT adds a new row to a table.

Example query INSERT INTO employees (employee_id, name, department_id, salary) VALUES (5, 'Priya', 20, 55000);

What happens?

A new employee named Priya is added.

Example result / note

This is a concept example; adapt the columns to your own table.

  1. UPDATE โ€” Changing Data

UPDATE changes existing rows. Always check the WHERE condition before running it.

Example query UPDATE employees SET salary = 58000 WHERE employee_id = 5;

What happens?

Only employee 5 is updated.

Example result / note

This is a concept example; adapt the columns to your own table.

  1. DELETE โ€” Removing Data

DELETE removes rows. Without WHERE, it can delete every row in the table.

The table requires a name, prevents duplicate emails, checks age, and supplies a default city.

Example result / note This is a concept example; adapt the columns to your own table.

  1. UNION vs UNION ALL

UNION combines compatible result sets and removes duplicates. UNION ALL keeps duplicates.

Example query SELECT city FROM customers UNION SELECT city FROM suppliers;

What happens?

Use UNION when duplicate results should be removed. Use UNION ALL when retaining duplicates is acceptable or

required.

Example result / note

This is a concept example; adapt the columns to your own table.

  1. Transactions โ€” COMMIT and ROLLBACK

A transaction groups database operations into a logical unit. COMMIT saves changes; ROLLBACK reverses

uncommitted changes.

Example query BEGIN; UPDATE employees SET salary = salary * 1. WHERE department_id = 10; ROLLBACK;

What happens?

The salary update is undone because ROLLBACK is issued before the transaction is committed.

Example result / note This is a concept example; adapt the columns to your own table.

SQL Interview Quick Revision

SELECT โ†’ retrieve data

WHERE โ†’ filter rows

GROUP BY โ†’ create groups

HAVING โ†’ filter groups

ORDER BY โ†’ sort results

JOIN โ†’ combine related tables

Subquery โ†’ query inside another query

CTE โ†’ named temporary query result

Window function โ†’ calculate across rows without collapsing them

PRIMARY KEY โ†’ uniquely identifies rows

FOREIGN KEY โ†’ connects related tables

COMMIT โ†’ save transaction changes

ROLLBACK โ†’ undo uncommitted changes

DELETE โ†’ remove rows

DROP โ†’ remove a database object

20 Common Interview Questions

1.What is SQL?

Answer: Structured Query Language used to work with relational data.

2.What is a primary key?

Answer: A unique, non-NULL identifier for rows.

3.What is a foreign key?

Answer: A key that references a key in another table.

4.WHERE vs HAVING?

Answer: WHERE filters rows; HAVING filters groups.

5.INNER JOIN vs LEFT JOIN?

Answer: INNER JOIN returns matches; LEFT JOIN keeps every left-side row.

6.What is NULL?

Answer: A missing or unknown value.

7.COUNT(*) vs COUNT(column)?

Answer: COUNT(*) counts rows; COUNT(column) ignores NULLs.

8.What is normalization?

Answer: Organizing data to reduce redundancy and anomalies.

9.What is an index?

Answer: A structure that can speed up data access but adds storage/write overhead.

10.What is a view?

Answer: A stored query exposed like a virtual table.

11.What is a subquery?

Answer: A query nested inside another query.

12.What is a CTE?

Answer: A named query result defined with WITH.

13.What is a window function?

Answer: A function that calculates across related rows while retaining row detail.

14.RANK vs DENSE_RANK?

Answer: RANK leaves gaps after ties; DENSE_RANK does not.

15.DELETE vs TRUNCATE?

Answer: DELETE can filter rows with WHERE; TRUNCATE removes all rows and behavior varies by DBMS.

16.DELETE vs DROP?

Answer: DELETE removes rows; DROP removes the database object.

17.What is ACID?

Answer: Atomicity, Consistency, Isolation and Durability.

18.What is SQL injection?