SQL 2
SQL 2
Q: How would you use a JOIN to combine data from two tables: one with employee information and
ON e.department_id = d.department_id;
Q: How can you handle NULL values in SQL when performing calculations?
A: Use the COALESCE() function to replace NULL with a default value, e.g., SELECT
COALESCE(column_name, 0) AS result;
A: WHERE filters rows before grouping, while HAVING filters groups after grouping.
department_id;
A: Use indexes, avoid SELECT *, optimize joins, analyze query plans, and reduce subqueries.
Q: Explain the purpose of indexes and how they improve query performance.
A: Indexes speed up data retrieval by allowing the database to find data without scanning every row.
Q: Write a query to create a new table with columns for employee ID, name, and salary.
A: CREATE TABLE employees (employee_id INT PRIMARY KEY, name VARCHAR(50), salary
DECIMAL(10, 2));
Q: How would you retrieve the top 5 highest-paid employees from an employee table?
A: SELECT * FROM employees ORDER BY salary DESC LIMIT 5;
Q: Write a query to delete duplicate rows from a table based on a specific column.
A: DELETE FROM employees WHERE id NOT IN (SELECT MIN(id) FROM employees GROUP BY
column_name);
Q: Explain the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.
A: INNER JOIN returns matching rows; LEFT JOIN includes unmatched rows from the left; RIGHT
JOIN includes unmatched rows from the right; FULL OUTER JOIN includes unmatched rows from
both.
department_id;
Q: How do you use the CASE statement in SQL, and provide an example?
A: CASE is used for conditional expressions, e.g., SELECT name, CASE WHEN salary > 5000
Q: Write a query to find employees who have not been assigned to any department.
A: A primary key uniquely identifies rows in a table, while a foreign key enforces relationships
between tables.
---
1. SELECT
---
2. FROM
---
3. WHERE
---
4. GROUP BY
---
5. HAVING
SELECT department, AVG(salary) FROM employees GROUP BY department HAVING AVG(salary) > 50000;
---
6. ORDER BY
---
7. LIMIT
---
8. OFFSET
Skips a specific number of rows before returning results. Often used with LIMIT.
Example:
SELECT * FROM customers LIMIT 10 OFFSET 5;
---
9. JOIN
INNER JOIN
LEFT JOIN
RIGHT JOIN
Example:
---
10. UNION
---
11. DISTINCT
---
12. IN
---
13. BETWEEN
---
14. LIKE
---
15. IS NULL / IS NOT NULL
---
16. CASE
SELECT name,
CASE
WHEN salary > 50000 THEN 'High'
ELSE 'Low'
END AS salary_category
FROM employees;
---
17. AS
---
18. WITH
---
19. EXISTS
---
20. ALL
SELECT * FROM products WHERE price > ALL (SELECT price FROM discounts);
---
21. ANY
SELECT * FROM products WHERE price > ANY (SELECT price FROM discounts);
---
22. INTERSECT
Returns rows that are present in both SELECT statements.
Example:
---
Returns rows from the first SELECT statement that are not in the second.
Example:
---
24. FETCH
SELECT * FROM employees ORDER BY salary DESC FETCH FIRST 5 ROWS ONLY;
---
25. PARTITION BY
---
26. PIVOT
Used to transform rows into columns (available in some SQL dialects like Oracle and SQL Server).
Example:
SELECT * FROM (
SELECT department, salary FROM employees
) PIVOT (
SUM(salary) FOR department IN ('HR', 'IT', 'Finance')
);
---
27. UNPIVOT
---
28. MERGE
29. APPLY
Allows applying a table-valued function or query to each row from another query (specific to SQL Server).
Example:
---
COMMANDS
These commands define the structure of the database, such as tables, indexes, and schemas.
TRUNCATE: Removes all rows from a table without logging individual row deletions.
---
INSERT INTO employees (id, name, age) VALUES (1, 'John Doe', 30);
---
COMMIT;
ROLLBACK;
SAVEPOINT: Sets a point within a transaction to which you can roll back.
SAVEPOINT save1;
---
---
These commands are often specific to database management systems or advanced SQL usage.
DESCRIBE employees;
SHOW TABLES;
USE company_db;
---
7. Advanced Commands
These are used for advanced database operations.
ANALYZE: Collects statistics about a table for query optimization (specific to some DBMS).