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

SQL Syntactical Execution Order

SQL Syntactical Execution Order
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)
12 views

SQL Syntactical Execution Order

SQL Syntactical Execution Order
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/ 12

SQL

Syntactical and
Execution Order
02 codebasics.io

SQL(Structured Query Language), is a powerful tool for managing and


manipulating databases.

A critical aspect of writing effective SQL queries involves understanding the


syntactical order of the query and the logical order in which the database
engine executes these commands.

This article will guide you through these concepts using a simple analogy,
followed by a detailed technical explanation and practical examples.

Syntactical Order Logical Execution Order

What is Syntactical Order and Logical Execution Order?


Imagine you are planning a grocery shopping trip. Initially, you create a list
of what you need to buy, which is called the syntactical order. Then, you
perform the actual shopping, which is called the execution order.

Note: We will be using an analogy that focuses on explaining the


syntactical and logical execution order in SQL by concentrating on
the English meaning of the functions rather than their actual
functions, making the order easier to memorize.
03 codebasics.io

Creating List: Syntactical Order


Select: What to Buy: List items you need, like milk, curd, ghee.
From: Which Store: Decide where to buy, such as D-mart.
Where: Choose the area where the store is located, let's say Kondapur.
Group By: To carry these items together, you need a trolley.
Having Brand: Let’s say you want to buy products from a particular
brand, such as Amul.
Order By: Let’s say you want to order them by ascending order of price
at the billing counter.
Limit: You want to limit yourself not to buy any products apart from
these 3.
04 codebasics.io

Shopping: Logical Execution Order


From: First you will visit the Dmart.
Where: Where: In the Kondapur location.
Group By: You will pick a trolly to pick up these items.
Having: Then you will go to the section where Amul products are
displayed.
Select: Next, you will pick up the items you listed: milk, curd, and ghee.
Order By: At the billing counter, you will order them by ascending price.
Limit: As you have limited yourself to buying only these three products,
you will exit the market without purchasing any other items.
05 codebasics.io

Let's explore how SQL queries are written and executed in actual
database environments.

Syntactical Order: This is the order in which we write SQL


commands.

Select: Specifies the columns to be returned.


From: Specifies the table from which to retrieve data.
Where: Applies filtering conditions.
Group By: Groups rows that have the same values in specified columns
into summary rows.
Having: Applies conditions to groups defined by the GROUP BY clause.
Order By: Specifies the sort order of the rows returned.
Limit: Specifies the maximum number of rows to return.

Logical Execution Order: This is the order in which the SQL engine
processes the query.

From: Identifies the table(s).


Where: Filters rows before grouping.
Group By: Groups rows after WHERE filters have been applied.
Having: Filters after groups have been formed.
Select: Selects data from the result set.
Order By: Orders the results in the specified order.
Limit: Limits the results to a specified number of rows.
06 codebasics.io

Example:
Consider the below table: Books
This dataset will be a bookstore inventory which lists various books, their
genres, prices, and sales data.
07 codebasics.io

SQL Query:
Let's formulate an SQL query to find the top 2 genres by their average price
where at least 100 units were sold in Kondapur, ensuring that the average
price is greater than $7.00 and ordering them by descending order.

Syntactical Order:

Step-by-Step Execution (Logical Execution Order):


08 codebasics.io

From Clause:
All entries from the Books table are considered.
09 codebasics.io

Where Clause:
Filters for 'Kondapur' and UnitsSold greater than 100.

Group By Clause:
Group by Genre.
Here we want to find top genres by their average price. Grouping is necessary
to calculate aggregate functions like AVG(Price) for each genre.
10 codebasics.io

Having Clause:
Filters groups where the average price is greater than $7.00:

Select Clause:
Selects Genre and the computed AVG(Price) (aliased as AveragePrice) for
the final output.

Order By Clause:
Orders the groups by AVG(Price) in descending order.
11 codebasics.io

Limit Clause:
Limits the result to the top 2 genres that meet the criteria.
E n a b l i n g C a r e e r s

Found this interesting?


To gain access to more valuable content,
join our WhatsApp Channel

SCAN TO JOIN

codebasics.io

You might also like