π From Joins to Indexes: Level Up Your SQL Skills with Real-World Examples!
SQL is the backbone of data manipulation and retrieval in software development. Whether you’re optimizing queries, designing databases, or preparing for interviews, mastering SQL is non-negotiable. This guide distills 20 critical SQL concepts into actionable insights, complete with code examples and best practices. Letβs dive in!
To retrieve the highest ID (useful for auto-incrementing keys):
-- Using MAX() SELECT MAX(id) AS last_id FROM employees; -- Using ORDER BY and LIMIT (MySQL, PostgreSQL) SELECT id FROM employees ORDER BY id DESC LIMIT 1;
Method 1: DISTINCT
SELECT DISTINCT * FROM employees;
Method 2: GROUP BY
SELECT name, department, COUNT(*) FROM employees GROUP BY name, department HAVING COUNT(*) > 1;
Method 3: CTE with ROW_NUMBER()
WITH CTE AS ( SELECT *, ROW_NUMBER() OVER (PARTITION BY name, department ORDER BY id) AS rn FROM employees ) DELETE FROM CTE WHERE rn > 1;
Returns matching rows from both tables:
SELECT e.name, d.department_name FROM employees e INNER JOIN departments d ON e.department_id = d.id;
All rows from the left table + matched rows from the right:
SELECT e.name, d.department_name FROM employees e LEFT JOIN departments d ON e.department_id = d.id;
Query hierarchical data (e.g., employee-manager relationships):
SELECT e1.name AS employee, e2.name AS manager FROM employees e1 LEFT JOIN employees e2 ON e1.manager_id = e2.id;
CREATE TABLE employees ( id INT PRIMARY KEY, -- Automatically clustered name VARCHAR(50) );
CREATE NONCLUSTERED INDEX idx_lastname ON employees (last_name);
Key Differences:
Clustered Index | Non-Clustered Index |
---|---|
One per table | Multiple allowed |
Data stored with index | Index stored separately |
SELECT name, salary, RANK() OVER (ORDER BY salary DESC) AS rank, DENSE_RANK() OVER (ORDER BY salary DESC) AS dense_rank FROM employees;
Traverse hierarchical data (e.g., organizational charts):
WITH RECURSIVE OrgChart AS ( SELECT id, name, manager_id FROM employees WHERE manager_id IS NULL -- Start with CEO UNION ALL SELECT e.id, e.name, e.manager_id FROM employees e INNER JOIN OrgChart oc ON e.manager_id = oc.id ) SELECT * FROM OrgChart;
Specify columns to reduce overhead:
SELECT id, name, department FROM employees;
Faster than IN for large datasets:
SELECT name FROM employees e WHERE EXISTS ( SELECT 1 FROM departments d WHERE d.id = e.department_id );
SELECT c.name FROM customers c LEFT JOIN orders o ON c.id = o.customer_id WHERE o.customer_id IS NULL;
Calculate metrics based on conditions:
SELECT SUM(CASE WHEN sale_date > '2023-01-01' THEN amount ELSE 0 END) AS total_sales, AVG(CASE WHEN region = 'West' THEN amount END) AS avg_west_sales FROM sales;
Function | Use Case |
---|---|
ROW_NUMBER() | Assign unique row numbers |
COALESCE() | Replace NULL with default values |
LEFT() / RIGHT() | Extract substrings |
COUNT(DISTINCT) | Count unique values |
PARTITION BY | Group data in window functions |
π Pro Tip: Use EXPLAIN ANALYZE
(PostgreSQL) or EXPLAIN
(MySQL) to debug query performance!
π Ready to Ace Your Next SQL Interview?
Bookmark this guide, practice the examples, and dominate data-driven challenges!
#SQL #Database #SoftwareEngineering #TechInterviews #DataScience