How SQL Queries work? Deep Dive into SQL Query Execution Order
SQL queries are executed in a specific order which may be different then what you think
Hello guys, one of the common question on technical interviews about SQL is how exactly SQL query work? While this may seems simple, many programmers including experienced one fail to answer this with confidence.
Many people don’t even know how the SQL commands are executed and in which order? does the WHERE executed first or HAVING? When is filtering happen?
For them the SQL query is executed as they are written but that’s not true, you can see from the below diagram that FROM and JOIN is executed before you can SELECT anything, which is again very rational if you think through.
Earlier, I have shared How Kafka works internally, and difference between Apache Kafka, and RabbitMQ and in this article, I am going to answer how exactly SQL query works under the hood, so stay tuned and continue reading.
And, if are preparing for tech interviews and you need more questions not just queries but also database and SQL related questions from other topics like indexes, joins, group by, aggregation, and window functions then you can also checkout Grokking the SQL Interview book
This book is one of the specially designed book to prepare you for SQL interviews by answering popular questions. You an also use discount code friends20 to get 20% discount now.
How exactly SQL Query executed?
Structured Query Language (SQL) is the standard language for managing and manipulating relational databases.
It provides a powerful and efficient way to interact with data, enabling developers, analysts, and data scientists to retrieve, insert, update, and delete information from databases.
While SQL queries are written in a human-readable format, there is a complex process that occurs behind the scenes to execute these queries and retrieve the desired results.
In this article, we’ll delve into the inner workings of SQL queries, breaking down the process step by step.
1. Query Parsing and Tokenization
The journey of an SQL query begins with parsing and tokenization. When a user submits an SQL query, the database management system (DBMS) must first break down the query into individual tokens.
Keep reading with a 7-day free trial
Subscribe to Javarevisited Newsletter to keep reading this post and get 7 days of free access to the full post archives.