0% found this document useful (0 votes)
5 views

SQL Concepts

Uploaded by

tmaniraj1111
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

SQL Concepts

Uploaded by

tmaniraj1111
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

SQL CONCEPTS AND

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.

DENSE_RANK: Similar to RANK but without gaps between ranks. If


two rows are tied at rank 1, the next rank will be 2.

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.

UNION: Combines the results of two queries vertically, merging


rows from two result sets with the same structure.

Key Distinction: JOIN is for combining data side-by-side, while


UNION is for stacking data on top of each other.

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.

DROP: Completely removes a table from the database, including


its structure and data. This action is irreversible.

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.

TEMP TABLE: A physical table stored temporarily in the database.


It can be used across multiple queries until the session ends.

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.

Reusability: CTEs can be referenced multiple times in a query,


while subqueries cannot be reused easily.

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.

COALESCE: Returns the first non-NULL value from a list of


expressions. It can handle multiple 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.

INNER JOIN: Combines rows from two tables based on a matching


condition. It can produce more detailed and structured results.

Key Difference: INTERSECT compares entire result sets for


commonality, while INNER JOIN matches rows based on specific
columns.

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

You might also like