0% found this document useful (0 votes)
4 views5 pages

SQL

The document outlines key differences between various SQL concepts, including JOIN types, filtering clauses, ranking functions, and data manipulation commands. It provides examples for INNER JOIN vs. LEFT JOIN, WHERE vs. HAVING, and explains the use of GROUP BY, DISTINCT, DELETE, TRUNCATE, UNION, COALESCE, EXISTS, and the CASE statement. Additionally, it highlights the importance of using the QUALIFY clause correctly and the distinctions between RANK(), DENSE_RANK(), and ROW_NUMBER().

Uploaded by

Hariom Damekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

SQL

The document outlines key differences between various SQL concepts, including JOIN types, filtering clauses, ranking functions, and data manipulation commands. It provides examples for INNER JOIN vs. LEFT JOIN, WHERE vs. HAVING, and explains the use of GROUP BY, DISTINCT, DELETE, TRUNCATE, UNION, COALESCE, EXISTS, and the CASE statement. Additionally, it highlights the importance of using the QUALIFY clause correctly and the distinctions between RANK(), DENSE_RANK(), and ROW_NUMBER().

Uploaded by

Hariom Damekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1️⃣ INNER JOIN vs.

LEFT JOIN
INNER JOIN → Only matching rows
LEFT JOIN → All left table rows + matching right rows

SELECT * FROM orders o


LEFT JOIN customers c ON o.customer_id = c.id;

2️⃣ WHERE vs. HAVING


❌ HAVING age > 30 (wrong without GROUP BY)
✅ WHERE age > 30 (before aggregation)
✅ HAVING COUNT(*) > 10 (after aggregation)
3️⃣ RANK() vs. DENSE_RANK() vs. ROW_NUMBER()
SELECT name, sales,
RANK() OVER (ORDER BY sales DESC),
DENSE_RANK() OVER (ORDER BY sales DESC),
ROW_NUMBER() OVER (ORDER BY sales DESC)
FROM sales_table;

4️⃣ QUALIFY Clause (Not in SQL Server!)


❌ QUALIFY RANK() OVER (ORDER BY sales) = 1;
✅ Use CTE instead:
WITH cte AS (
SELECT *, RANK() OVER (ORDER BY sales) AS rnk
FROM sales_table
)
SELECT * FROM cte WHERE rnk = 1;
5️⃣ GROUP BY vs. DISTINCT
GROUP BY → Aggregates
DISTINCT → Unique rows

SELECT DISTINCT department FROM employees;

6️⃣ DELETE vs. TRUNCATE


DELETE FROM table WHERE id = 5; (removes specific
rows, can be rolled back)
TRUNCATE TABLE table; (removes all rows, faster, cannot
rollback)
7️⃣ UNION vs. UNION ALL
UNION → Removes duplicates
UNION ALL → Keeps all rows

SELECT city FROM customers


UNION ALL
SELECT city FROM suppliers;

8️⃣ COALESCE vs. ISNULL


COALESCE(a, b, c) → Returns first non-null value
ISNULL(a, default) → Works only in SQL Server
9️⃣ EXISTS vs. IN
EXISTS is faster for subqueries returning many values
IN is better for small lists

SELECT * FROM orders o


WHERE EXISTS (SELECT 1 FROM customers c WHERE c.id
= o.customer_id);

🔟 CASE Statement (Avoid ELSE NULL confusion!)


SELECT name,
CASE WHEN age < 18 THEN 'Minor'
WHEN age >= 18 THEN 'Adult'
ELSE 'Unknown' END AS category
FROM users;

You might also like