






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







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.
Example query SELECT name, salary FROM employees;
What happens?
Example result / note name | salary Ravi | 50000 Anu | 60000 Kiran | 70000 Meena | 45000
Example query SELECT name, salary FROM employees WHERE salary > 55000;
What happens?
Example result / note
Anu | 60000 Kiran | 70000
Example query SELECT DISTINCT department_id FROM employees;
What happens?
Example result / note 10 20 30
Example query SELECT name, salary FROM employees ORDER BY salary DESC;
What happens?
Example result / note Kiran | 70000 Anu | 60000 Ravi | 50000 Meena | 45000
Example query SELECT name FROM employees WHERE name LIKE 'K%';
What happens?
Example result / note
Kiran
Example result / note employee_count = 4
Example query SELECT SUM(salary) AS total, AVG(salary) AS average, MIN(salary) AS lowest, MAX(salary) AS highest FROM employees;
What happens?
Example result / note total = 225000 average = 56250 lowest = 45000 highest = 70000
Example query SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id;
What happens?
Example result / note 10 | 60000 20 | 60000 30 | 45000
Example query SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id HAVING AVG(salary) > 50000;
What happens?
Example result / note 10 | 60000
Example query SELECT e.name, d.department_name FROM employees e INNER JOIN departments d ON e.department_id = d.department_id;
What happens?
Example result / note Ravi | IT Anu | HR Kiran | IT Meena | Sales
Example query SELECT d.department_name, e.name FROM departments d LEFT JOIN employees e ON d.department_id = e.department_id;
What happens?
Example result / note IT | Ravi IT | Kiran HR | Anu Sales | Meena
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?
Example result / note Anu | 60000 Kiran | 70000
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?
Example result / note Ravi | 50000 | Medium Anu | 60000 | High Kiran | 70000 | High Meena | 45000 | Low
Example query SELECT name, department_id, salary, RANK() OVER ( PARTITION BY department_id ORDER BY salary DESC ) AS salary_rank FROM employees;
What happens?
Example result / note IT: Kiran 1, Ravi 2 HR: Anu 1 Sales: Meena 1
Example query SELECT name, salary, ROW_NUMBER() OVER (ORDER BY salary DESC) AS row_no FROM employees;
What happens?
Example result / note
This is a concept example; adapt the columns to your own table.
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?
Example result / note
This is a concept example; adapt the columns to your own table.
Example query INSERT INTO employees (employee_id, name, department_id, salary) VALUES (5, 'Priya', 20, 55000);
What happens?
Example result / note
This is a concept example; adapt the columns to your own table.
Example query UPDATE employees SET salary = 58000 WHERE employee_id = 5;
What happens?
Example result / note
This is a concept example; adapt the columns to your own table.
Example result / note This is a concept example; adapt the columns to your own table.
Example query SELECT city FROM customers UNION SELECT city FROM suppliers;
What happens?
Example result / note
This is a concept example; adapt the columns to your own table.
Example query BEGIN; UPDATE employees SET salary = salary * 1. WHERE department_id = 10; ROLLBACK;
What happens?
Example result / note This is a concept example; adapt the columns to your own table.
SQL Interview Quick Revision
20 Common Interview Questions
Answer: Structured Query Language used to work with relational data.
Answer: A unique, non-NULL identifier for rows.
Answer: A key that references a key in another table.
Answer: WHERE filters rows; HAVING filters groups.
Answer: INNER JOIN returns matches; LEFT JOIN keeps every left-side row.
Answer: A missing or unknown value.
Answer: COUNT(*) counts rows; COUNT(column) ignores NULLs.
Answer: Organizing data to reduce redundancy and anomalies.
Answer: A structure that can speed up data access but adds storage/write overhead.
Answer: A stored query exposed like a virtual table.
Answer: A query nested inside another query.
Answer: A named query result defined with WITH.
Answer: A function that calculates across related rows while retaining row detail.
Answer: RANK leaves gaps after ties; DENSE_RANK does not.
Answer: DELETE can filter rows with WHERE; TRUNCATE removes all rows and behavior varies by DBMS.
Answer: DELETE removes rows; DROP removes the database object.
Answer: Atomicity, Consistency, Isolation and Durability.