0% found this document useful (0 votes)
14 views1 page

1 Page Table - Which SQL Clause To Use To Solve A Question

The document provides a comprehensive guide on SQL clauses and commands, detailing their specific scenarios and applications. It covers various functions such as JOINs, aggregate functions, window functions, and sub-queries, explaining how they can be used to manipulate and retrieve data effectively. Additionally, it includes examples for clarity on self joins, unions, common table expressions (CTEs), and views.

Uploaded by

book95761
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)
14 views1 page

1 Page Table - Which SQL Clause To Use To Solve A Question

The document provides a comprehensive guide on SQL clauses and commands, detailing their specific scenarios and applications. It covers various functions such as JOINs, aggregate functions, window functions, and sub-queries, explaining how they can be used to manipulate and retrieve data effectively. Additionally, it includes examples for clarity on self joins, unions, common table expressions (CTEs), and views.

Uploaded by

book95761
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/ 1

Which Clause/Command to Use in Particular Scenario

Clause/ Command Scenario


FROM Get Data from a Particular Table
SELECT Display certain Columns
WHERE To display only Filtered Rows
CASE To display both Filtered and Unfiltered Rows
When <Condition> Then <Value 1>
Else <Value 2>
End as <New Column Name>

ORDER BY Sort the Output Rows


LIMIT Specify how many Output Rows should be displayed
OFFSET Skip certain number of initial rows of the Output rows

Sub-Query in WHERE clause Inner query’s Output is needed for Filtering Outer Query Table
Sub-Query in FROM clause Inner query’s Output needs to be displayed

Correlated Sub-Query -> Looping Inner Query The inner query is executed once for each row of outer query
It can be in SELECT clause or WHERE clause Can do the work of Window Function

JOIN Horizontally Merge multiple Related tables


INNER JOIN Merge only Matching Rows
LEFT JOIN Merge all rows of Left Table & Matching Rows of Right Table
RIGHT JOIN Merge all rows of Right Table & Matching Rows of Left Table
OUTER JOIN Merge both Matching and Non-Matching Rows

SELF JOIN
SELF JOIN -> One Row JOINED to One Row Self-Referencing Table. Table has Relationship with itself.
Example -> Question related to employee and his manager
SELF JOIN -> One Row JOINED to Many Rows Example -> List of possible couples from Matrimony data table

Aggregate Functions Aggregate Entire Table into 1 row


GROUP BY and Aggregate Functions Aggregate some rows to get Consolidated rows
GROUP BY + HAVING and Aggregate Functions To Filter on the basis of Aggregated/Consolidated row info
Get Items Count of Flexibly Merged Groups SUM ( IF (Profession in (‘Army’, ‘Navy’), 1, 0) as Defense_count
Window Functions Display both Row level data & Aggregated/Analytical data in a Row
Function (..) Basic Syntax
Over (Partition by ….. Order by … Rows Between .. )
Aggregate Functions Display both Row level data & Aggregated data in a Row
RANK, ROW_NUMBER, DENSE_RANK Rank the rows based on value in 1 or more Column(s)
NTILE Divide rows into Categories based on value in 1 or more Column(s)
LAG, LEAD To show data from next/previous row in a row

To get Aggregated or Analytical data from certain range of Preceding,


Window Frame Current and Following rows.
Modifies the basic frame created by ‘Partition by’ clause
FIRST_VALUE, LAST_VALUE, NTH_VALUE Get First, Last or Nth value in a Frame

Different Window Frames Scenarios to use these Window Frames


Unbounded Preceding and Unbounded Following Get Largest or Smallest Value in a Frame
Unbounded Preceding and Current Row Get Cumulative/Running Sum (Default Frame)
1 Preceding and 1 Following Moving Window Frame of 3

UNION Vertically Merge multiple tables having same Columns


CTE To simplify a Complex query with many Sub-Queries
VIEWs For Frequently used Query AND For Access management

You might also like