SQL Statements | SQL Select Statement | Step-by-step Execution of Select Statement | Data Query Language

Execution of Select Statement
  • SQL Select Statement Execution:

In This article I will explain about all the different SQL Statements which are DDL,DML an DCL.The main focus is on SQL Select Statement execution.

1.DDL – Data Definition Language:

Command Description
CREATE Creates a new table, a view of a table, or other object in database
ALTER Modifies an existing database object, such as a table.
DROP Deletes an entire table, a view of a table or other object in the database.


2.DML – Data Manipulation Language:

Command Description
INSERT Creates a record
UPDATE Modifies records
DELETE Deletes records


3.DCL – Data Control Language:

Command Description
GRANT Gives a privilege to user
REVOKE Takes back privileges granted from user


4.DQL – Data Query Language:

Command Description
SELECT Retrieves certain records from one or more tables
  • SQL Select Statement/Starting with Select Statement in SQL:

Select statement in SQL is used to fetch the records from the table.To select the data from the database table SQL select statements are used.The actual result is stored in to Result table which is called as resultset which is stored in cache not in the main memory.We can fetch the records from the table by using Β types:

SQL Statements - Select Statement

Select Syntax by using ‘*’:

Select * from Table_name;

Select Syntax by using column names:

Select column_name1,column_name2 from table_name;

As considering the performance matrices the column name syntax is better than select * syntax.

Real Life Scenarios for SQL Select Statement:

Select statement is most common and very widely used sql statement.So there should be millions of scenarios of Select statement.I am explaining the basic scenarios of select statement.

Consider following table named ‘Employee’:

Employee_num Employee_name Department ID Salary
1 Amit 100 680000
2 Rohan 100 550000
3 Rohit 101 430000

1.Scenario 1:

List the all information about Employee?

Query used:

Select * from Employee;

Select Employee_num,Employee_name,Department_ID,Salary from Employee;

The above 2 statement fetches all records of Employee.

2.Scenario 2:

Select the Employees whos salary is greater than 500000?

Query used:

Select * from Employee where salary > 500000;

Select Employee_num,Employee_name,Department_ID,Salary from Employee where salary>500000;


Employee_num Employee_name Department ID Salary
1 Amit 100 680000
2 Rohan 100 550000

3.Scenario 3:

Fetch the departmentwise total salary.


Select Department ID,Sum(Salary) from Employee group by Department_ID;


Department ID Salary
100 1230000
101 430000

4.Scenario 4:

Select Departmentwise sum of salary where department_id is 100;


Select Department ID,Sum(Salary) from Employee group by Department_ID

having department_id=100;


Department ID Salary
100 1230000


  • Step-by-step Execution of Select Statement:

Here we are taking following query for internal execution explaination:

Select Department ID,Sum(Salary) from Employee group by Department_ID

having department_id=100;

Step 1:Β From Clause Execution:

In Every select statement first step is execution of the statements in from clause.In above statement there is only one table so it directly takes the table.So in above statement first step optimizer or compiler points to table name Employee.

Step 2:Where Clause:

The second step is where clause.When there is no where clause in Select statement optimizer directly fetches all the results from specified table.Where clause filters the data in database.In above query there is no where condition so Optimizer directly takes all the records from the table.

Step 3:Group By clause

The Step 3 is Group by clause.If the query contains group by clause then the specified result of Query is partitioned in to different groups.One group is every combination of values given in to group by clause.In above query we are doing grouping the department id wise.

Step4:Having Clause

If your Query contains having clause then it will executes after the group by clase.After grouping if user wants to filter the records then having conditios comes in picture.In our query we are taking sum of records having department id=100.

Step 5:Aggregate functions and select columns:

After fetching all the records and executing the clauses the columns which are taken in select statement gets executed.The funnctions like max,min,count,sum is executed first and then all columns taken in select statement gets executed last.

Execution of Select StatementHope everyone gets idea about select statement and execution of select statement.Please comment below if you have any queries or if you like article.

>>> >>>>Click Here to Get SQL Select Statement execution PDF<<<<<<<<<


About admin 111 Articles
Oracle Consultant with Vast Experience in Oracle BI and SQL PLSQL Development


    • Thanks Bhaskar..If you want any information on Any topic on SQL..Do let me knowwww…I will write article on it in detail πŸ™‚

  1. Admin – Thanks for the post. Does “Order by” clause get execute before or after the select columns? I guess “Order by” would execute prior to the select columns.

    • No Buddy..Order by is executed after selecting all the records from the table.Once all records are fetched in memory buffeer after that order by program(internal) executes and orders the result..

10 Trackbacks / Pingbacks

  1. Parser and Optimizer in SQL |Execution Plan in SQL | SQL Performance Tuning |Difference Between Rule Based and Cost Based Optimizer
  2. Basic SQL Commands | SQL Commands with Real Life Examples | Data Definition Language | Create table Using Multiple ways
  3. Query to find second highest salary of employee? | How to find second highest salary of employee?
  4. SQL Interview Questions for Tech Mahindra | Important SQL Interview Questions
  5. Interview Questions For Oracle | SQL interview Questions
  6. Basic Unix Commands | Frequently Used Unix Commands | Unix Commands with Examples | Useful Unix Commands
  7. Chmod command in unix | Unix File Permissions | chmod with examples | Chwn command | Chgrp command | UNMASK
  8. Database Normalization Examples | Normalization in Database with Examples | Database Normalization with Real Life Examples | Designing Normalized Database
  9. Difference between Truncate and Delete, Drop in SQL | Truncate Delete and Drop Commands in SQL | SQL Difference between Truncate,Delete and Drop Commands | Real Life Examples of Truncate Delete and Drop Command
  10. Interview Questions for TCS | SQL Interview Questions for TCS | Interview Questions Asked in TCS | TCS Interview Questions

Comments are closed.