📚 Topic-wise SQL Notes for HackerRank
Certifications
1. SELECT Statements
The SELECT statement is used to retrieve data from a database. Basic structure:
SELECT column1, column2 FROM table_name;
2. WHERE Clause
Used to filter records. It extracts only those records that fulfill a specified condition.
SELECT * FROM employees WHERE salary > 50000;
3. DISTINCT Keyword
Removes duplicate values from the result.
SELECT DISTINCT department FROM employees;
4. ORDER BY (ASC/DESC)
Sorts the result set in ascending (default) or descending order.
SELECT * FROM employees ORDER BY salary DESC;
5. LIMIT and OFFSET
LIMIT restricts the number of rows returned.
OFFSET skips a specific number of rows.
SELECT * FROM employees LIMIT 5 OFFSET 10;
6. AND, OR, NOT
Combines multiple conditions in WHERE.
SELECT * FROM employees WHERE salary > 30000 AND department = 'IT';
7. Aggregate Functions
COUNT(), SUM(), AVG(), MIN(), MAX() are used with GROUP BY to perform operations on
groups.
SELECT department, AVG(salary) FROM employees GROUP BY department;
8. GROUP BY and HAVING
GROUP BY groups rows sharing a property.
HAVING filters groups (like WHERE for GROUP BY).
SELECT department, COUNT(*) FROM employees GROUP BY department HAVING
COUNT(*) > 5;
9. JOINs (INNER, LEFT, RIGHT, FULL)
- INNER JOIN: matches rows in both tables.
- LEFT JOIN: all records from left, matched from right.
- RIGHT JOIN: all from right, matched from left.
- FULL JOIN: all from both, matched where possible.
SELECT e.name, d.name FROM employees e INNER JOIN departments d ON e.dept_id = d.id;
10. Subqueries
A subquery is a query nested inside another query.
SELECT name FROM employees WHERE dept_id = (SELECT id FROM departments WHERE
name = 'Sales');
11. CASE Statements
Used to apply conditional logic.
SELECT name, salary,
CASE
WHEN salary > 100000 THEN 'High'
ELSE 'Low'
END AS salary_grade FROM employees;
12. DATE Functions
YEAR(), MONTH(), NOW(), DATEDIFF(), etc.
SELECT DATEDIFF(NOW(), hire_date) FROM employees;
13. CTE (WITH Clause)
Improves readability of complex queries.
WITH TopSales AS (
SELECT name, RANK() OVER (ORDER BY sales DESC) as rnk FROM employees
)
SELECT * FROM TopSales WHERE rnk <= 5;
14. Window Functions
Functions like RANK(), ROW_NUMBER(), DENSE_RANK(), LEAD(), LAG() that operate over a
window of rows.
SELECT name, department, RANK() OVER (PARTITION BY department ORDER BY salary
DESC) FROM employees;
15. UNION, INTERSECT, EXCEPT
Used to combine results of two SELECTs.
- UNION: combines and removes duplicates.
- INTERSECT: returns common records.
- EXCEPT: records in first but not in second.
16. Self JOIN
A table joined with itself using aliases.
SELECT A.name, B.name FROM employees A, employees B WHERE A.manager_id = B.id;
17. Views
A virtual table based on SQL query.
CREATE VIEW SalesDept AS SELECT * FROM employees WHERE department = 'Sales';
18. Indexes (Conceptual)
Used to speed up retrieval. Not part of most HackerRank problems, but good to know.
CREATE INDEX idx_name ON employees(name);