SQL Concepts
SQL Concepts
THEIR DIFFERENCES
www.ndredtech.com
@ndr_edtech
🔰 RANK VS DENSE_RANK
RANK: Assigns a rank to each row within a partition of the result
set, with gaps in the ranking when there are ties. For example, if
two rows are tied at rank 1, the next rank will be 3.
WHEN TO USE: Use RANK if you need to account for gaps in the
ranking and DENSE_RANK when you need sequential ranking
without gaps.
www.ndredtech.com
@ndr_edtech
🔰 WHERE VS. HAVING CLAUSE
WHERE: Filters rows before any grouping occurs. It is used for
filtering individual rows.
HAVING: Filters groups after aggregation. Use this when you need
to filter based on aggregate functions, like SUM, AVG, etc.
WHEN TO USE: Use WHERE for filtering raw data and HAVING for
filtering aggregated results.
www.ndredtech.com
@ndr_edtech
🔰 UNION VS. UNION ALL
UNION: Combines the results of two queries and removes
duplicate records.
UNION ALL: Combines the results of two queries and includes all
duplicates.
WHEN TO USE: Use UNION when you need distinct records and
UNION ALL when duplicates are acceptable for performance
gains.
www.ndredtech.com
@ndr_edtech
🔰 JOIN VS. UNION
JOIN: Combines columns from two tables based on a related
column between them. Used for horizontally merging data.
www.ndredtech.com
@ndr_edtech
🔰 DELETE VS. DROP VS. TRUNCATE
DELETE: Removes specific rows from a table based on a condition
and can be rolled back if within a transaction.
TRUNCATE: Removes all rows from a table but retains the table
structure for future use. It cannot be rolled back.
When to Use: Use DELETE when you want to remove specific rows,
DROP when you no longer need the table, and TRUNCATE when
you need to clear all data but keep the table.
www.ndredtech.com
@ndr_edtech
🔰 CTE VS. TEMP TABLE
CTE (Common Table Expression): A temporary result set that
only exists within the scope of a single query. It improves
readability and can be used multiple times in the same query.
When to Use: Use CTE for simpler, reusable logic within a single
query. Use TEMP TABLE when the data needs to be reused across
multiple queries.
www.ndredtech.com
@ndr_edtech
🔰 SUBQUERIES VS. CTES
Readability: CTEs make the code cleaner and easier to read,
especially when handling complex queries. Subqueries can be
more difficult to follow.
When to Use: Prefer CTEs when you need clarity and reusability;
use subqueries for simpler, one-off filtering or calculations.
www.ndredtech.com
@ndr_edtech
🔰 ISNULL VS. COALESCE
ISNULL: Returns a specified value if the expression is NULL,
otherwise returns the expression itself. It only takes two
arguments.
When to Use: Use ISNULL when you only need a simple null check.
Use COALESCE for more complex scenarios with multiple
potential null values.
www.ndredtech.com
@ndr_edtech
🔰 INTERSECT VS. INNER JOIN
INTERSECT: Returns common rows between two result sets. Both
queries must have the same columns and data types.
www.ndredtech.com
@ndr_edtech
🔰 EXCEPT VS. NOT IN
EXCEPT: Returns rows from the first query that are not in the
second query. It compares entire result sets.
NOT IN: Filters rows from a table based on values not present in a
subquery. It is used in a WHERE clause and compares individual
values.
When to Use: Use EXCEPT for full-set differences and NOT IN for
filtering based on a specific column.
www.ndredtech.com
@ndr_edtech
Thank You
FOLLOW FOR MORE
@ndr_edtech
www.ndredtech.com