SQL Queries
SQL Queries That Will Surprise You! ๐๐ก
SQL is the backbone of data manipulation, but some queries are not your everyday SELECT *
. ๐คฏ Whether youโre a beginner or a seasoned developer, these mind-bending SQL queries will leave you astonished. Letโs dive in! ๐
1. Finding the Second Highest Salary ๐
Have you ever struggled to find the runner-up salary in a table? Hereโs how to do it without using LIMIT
or OFFSET
:
SELECT MAX(salary) AS second_highest_salary
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
Explanation:
- The inner query finds the highest salary.
- The outer query finds the maximum salary that is less than the highest.
2. Detecting Missing Gaps in a Sequence ๐
To find missing IDs in a sequence:
SELECT t1.id + 1 AS missing_id
FROM table_name t1
LEFT JOIN table_name t2
ON t1.id + 1 = t2.id
WHERE t2.id IS NULL;
Explanation:
- Join each row to the next expected row.
- Filter out rows where the next ID exists.
3. Finding the Nth Highest Value ๐ฅ
If you need the nth highest value, hereโs a general approach:
SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET n - 1;
Explanation:
- Use
ORDER BY
to sort salaries. - Skip
n-1
rows usingOFFSET
.
4. Calculating a Running Total ๐งฎ
To compute a cumulative sum in SQL:
SELECT employee_id, salary,
SUM(salary) OVER (ORDER BY employee_id) AS running_total
FROM employees;
Explanation:
- The
SUM()
function with theOVER
clause adds up salaries in order. ORDER BY
defines the sequence for the running total.
5. Pivoting Data Dynamically ๐
Transform rows into columns:
SELECT employee_id,
MAX(CASE WHEN month = 'January' THEN sales END) AS january_sales,
MAX(CASE WHEN month = 'February' THEN sales END) AS february_sales
FROM sales
GROUP BY employee_id;
Explanation:
- Use
CASE
to pivot specific rows into columns. MAX
ensures unique values per column.
6. Finding Duplicates ๐ฏ
To identify duplicate entries:
SELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name
HAVING COUNT(*) > 1;
Explanation:
- Group rows by the column youโre checking.
- Use
HAVING
to filter groups with multiple rows.
7. Finding Overlapping Date Ranges ๐
When you need to detect overlapping time periods:
SELECT a.*, b.*
FROM reservations a
JOIN reservations b
ON a.start_date < b.end_date
AND a.end_date > b.start_date
AND a.id <> b.id;
Explanation:
- Check if the date ranges intersect.
- Exclude the same row from matching itself.
8. Hierarchical Queries (Finding Manager Trees) ๐ฒ
To get the hierarchy of employees and their managers:
WITH RECURSIVE employee_tree AS (
SELECT employee_id, manager_id, 1 AS level
FROM employees
WHERE manager_id IS NULL
UNION ALL
SELECT e.employee_id, e.manager_id, et.level + 1
FROM employees e
JOIN employee_tree et
ON e.manager_id = et.employee_id
)
SELECT * FROM employee_tree;
Explanation:
- Use a recursive CTE to traverse the hierarchy.
- The base case includes top-level managers, and recursion builds levels.
9. Generating Random Data ๐ฒ
To populate a table with random values:
INSERT INTO random_data (value)
SELECT FLOOR(1 + (RAND() * 100))
FROM numbers_table
LIMIT 10;
Explanation:
RAND()
generates random numbers.- Multiply and floor values to get integers in a range.
10. Finding the Median ๐
Calculating the median value in SQL can be tricky:
SELECT AVG(salary) AS median
FROM (
SELECT salary
FROM employees
ORDER BY salary
LIMIT 2 - (SELECT COUNT(*) FROM employees) % 2
OFFSET (SELECT (COUNT(*) - 1) / 2 FROM employees)
) subquery;
Explanation:
- Find the middle values using
LIMIT
andOFFSET
. - Average them if necessary to compute the median.
Conclusion ๐
SQL is full of hidden gems that can solve complex problems elegantly. ๐ With these queries in your arsenal, youโll tackle any data challenge like a pro. Happy querying! ๐ปโจ
Have a query thatโs stumped you? Share it in the comments below! ๐
© Lakhveer Singh Rajput - Blogs. All Rights Reserved.