SQL Query Execution Order: How SQL Queries Actually Work (Must Know for Interviews)

Disclosure: This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.

SQL query execution order

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 developer don't even know how the SQL commands are executed and in which order?

For them the SQL query is executed as they are written but that's not true, you can see from the above 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 20 SQL queries from interviews and 50 System design questions 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 these 200+ SQL Interview Questions .

This course is one of the specially designed course to prepare you for SQL interviews by answering popular questions. You can also get this for big discount now.

How exactly SQL Query is executed?

Structured Query Language or 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 declarative, 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.

Tokens are the smallest units of the query and can include keywords (SELECT, FROM, WHERE, etc.), table and column names, operators (=, >, <, etc.), and values.

This process involves identifying the syntax and structure of the query to ensure it follows the rules of the SQL language.

how SQL query are executed


2. Query Optimization

Once the query is parsed and tokenized, the DBMS performs query optimization. This is a crucial step that aims to improve the efficiency of query execution.

The DBMS analyzes the query and explores various execution plans to determine the most efficient way to retrieve the requested data.

It considers factors such as indexes, table relationships, and available resources to create an execution plan that minimizes the time and resources needed to complete the query.

how to do query optimization


3. Execution Plan Generation

The chosen execution plan outlines the sequence of steps required to fulfill the query.

It determines the order in which tables are accessed, the types of joins performed, and the filtering conditions applied.

The DBMS generates this plan based on statistical information about the data distribution and the database schema.

The goal is to reduce the amount of data that needs to be processed and to optimize disk and memory usage.

On Microsoft SQL Server, a Query Execution plan looks like below:

how Execution plan looks like


4. Data Retrieval and Joins

With the execution plan in place, the DBMS begins the process of data retrieval. If the query involves multiple tables, the DBMS performs join operations to combine the relevant data.

Joining tables efficiently requires comparing and matching rows based on specified conditions. Depending on the type of join (inner join, outer join, etc.), the DBMS determines which rows from each table should be included in the result set.

How SQL join works


5. Filtering and Sorting

After joining the necessary tables, the DBMS applies filtering conditions specified in the WHERE clause. This involves evaluating each row to determine whether it meets the criteria set by the user.

Rows that do not satisfy the conditions are discarded, while those that pass the filter are retained for further processing.

Additionally, if the query includes an ORDER BY clause, the DBMS will sort the resulting rows based on the specified column(s).

Sorting involves arranging the data in a specific order, such as ascending or descending, to produce the final ordered result set.

When does filtering and sorting happens in SQL Query Execution


6. Aggregation and Grouping

Aggregation functions such as SUM, COUNT, AVG, MIN, and MAX are commonly used in SQL queries to perform calculations on groups of data.

If the query includes a GROUP BY clause, the DBMS groups the rows based on the specified columns. It then applies the aggregation functions to each group separately, producing summary statistics or calculations for the grouped data.

Aggregation and Grouping in sQL query


7. Result Set Generation

With all the necessary operations performed, the DBMS generates the final result set. This set of rows and columns represents the data that satisfies the user's query. T

he result set is then returned to the user or the application that initiated the query.

when is result generated from SQL query


8. Index Utilization

Indexes play a vital role in optimizing the performance of SQL queries. An index is a data structure that provides a quick way to look up data based on specific columns.

When executing a query, the DBMS may utilize indexes to efficiently locate the relevant rows, reducing the need for full-table scans and improving query response times.

Index Utilization in SQL query


9. Transaction Management

Transactional operations in SQL, such as INSERT, UPDATE, and DELETE, involve modifying data in the database. These operations are grouped into transactions, which ensure data consistency and integrity.

When a transaction is initiated, the DBMS may lock the affected rows or tables to prevent other transactions from accessing or modifying them concurrently.

Once the transaction is completed, the changes are either committed to the database or rolled back, depending on the success or failure of the transaction.

Transaction Management in SQL


10. Caching and Memory Management

Modern database systems employ various caching and memory management techniques to optimize query performance.

Caching involves storing frequently accessed data in memory to reduce the need for disk reads, which are slower in comparison.

The DBMS may also use buffer pools to manage memory allocation for query execution and result set generation, further enhancing efficiency.

Caching and Memory Management in SQL


SQL Query Order? How SQL Query are executed under the hood?

It's also important to know and remember in which order various SQL commands like SELECT, FROM, COUNT, WHERE, HAVING, ORDER BY, JOIN etc are applied

SQL queries are processed in a specific order, and understanding this order is crucial for writing and optimizing queries effectively. The typical order of SQL query processing involves the following steps:

  1. FROM: The query begins by specifying the source tables or views from which the data will be retrieved. This clause defines the primary data source for the query.

  2. JOIN: If the query involves multiple tables, the JOIN clause is used to combine data from different tables based on specified conditions. Different types of joins (INNER JOIN, LEFT JOIN, RIGHT JOIN, etc.) determine how rows from each table are matched and included in the result set.

  3. WHERE: The WHERE clause is used to filter rows based on specific conditions. It restricts the data to only those rows that meet the specified criteria. Rows that do not satisfy the conditions are excluded from further processing.

  4. GROUP BY: If aggregation is required, the GROUP BY clause is used to group rows with similar values in specified columns. This step is often used in conjunction with aggregation functions like COUNT, SUM, AVG, etc. to perform calculations on grouped data.

  5. HAVING: The HAVING clause is used to filter the result set after the GROUP BY operation has been performed. It specifies conditions for filtering aggregated data. Similar to the WHERE clause, rows that do not meet the criteria are excluded from the final result.

  6. SELECT: The SELECT clause is used to specify the columns that should appear in the final result set. It determines which data will be retrieved and displayed in the query output.

  7. DISTINCT: The DISTINCT keyword, if used, removes duplicate rows from the result set, ensuring that only unique values are displayed.

  8. ORDER BY: The ORDER BY clause is used to sort the result set based on specified columns. It arranges the rows in ascending or descending order, as specified.

  9. LIMIT/OFFSET or FETCH/FIRST: Depending on the database system, you might use LIMIT (or FETCH or FIRST) and OFFSET clauses to control the number of rows returned and to implement pagination.

  10. UNION/INTERSECT/EXCEPT: If needed, these set operations can be used to combine the results of multiple queries.

Here is a nice diagram from Medium which clearly explains how the SQL query looks like and how its executed by Query engine:

SQL Query execution order

It's important to note that the actual order of execution may vary based on the specific database management system being used.** However, the logical processing order remains consistent across most SQL databases.

Additionally, modern query optimizer may rearrange some of these steps for performance reasons while ensuring that the final result remains accurate and consistent.

Understanding the order of SQL query processing not only help in technical interviews but also allows you to write efficient and effective queries, and it provides insights into query optimization and performance tuning.

By structuring your queries with this order in mind, you can better control the flow of data and achieve the desired results.

Conclusion

That's all about how SQL query are executed under the hood. SQL queries might seem like simple statements, but there is a complex process that unfolds behind the scenes to retrieve, manipulate, and manage data.

From parsing and optimization to execution plan generation and result set generation, every step is meticulously orchestrated to ensure efficient and accurate query processing.

Understanding how SQL queries work under the hood provides developers and database administrators with valuable insights into performance optimization and query tuning, ultimately leading to better utilization of database resources and improved application responsiveness.

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 read Grokking the SQL Interview book or join 200+ SQL Interview Questions .

Both are great resources to prepare you for SQL interviews by answering popular questions.

All the best !!

    Top 8 System Design Interview Problems and Solutions for Practice

    Disclosure: This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.


    cover image_credit --- Educative

    Hello friends, System design is one of the biggest hurdle in getting job at Amazon, Google, or Netflix, and most of the startups. I have seen many experienced developer with years of experience in their resume falling apart in this round because of lack of preparation and knowledge.

    That's why its extremely important to prepare well for interviews and its best to prepare the frequently asked System design problems like how to design YouTube, and how to design WhatsApp to start with.

    By the way, if you are preparing for System design interviews and want to learn System Design in depth then you can also checkout sites like ByteByteGoDesignGuruExponentEducative Codemia.io, Bugfree.ai, and Udemy which have many great System design courses and if you need free system design courses you can also see the below article.

    Here are my favorite System design problems you can also use to start with, I have also linked to detailed article where you can find solution but I suggest you try on your own before looking at them:

    My Favorite System Design Problems to Crack Interview
    image_credit --- bytebytego

    Top 6 System Design Interview Problems for Practice

    Here a couple of my favorite System design questions to learn essential System design concepts and principles

    1. Design YouTube or any other video Streaming service like Netflix, HBO, or Amazon Prime Video(Solution)

    We all have used YouTube and best thing about this question is that we all are familiar with most of functionality but when it comes to designing them its tricky but since we are only considering about technical aspect and not functional.

    Designing a video streaming service akin to YouTube, Netflix, or Amazon Prime Video involves a multifaceted approach encompassing various components to ensure seamless content delivery, user engagement, and scalability.

    Commencing with user authentication and authorization, the system would employ secure methods like OAuth to safeguard user accounts. The heart of the service would be a highly scalable and distributed video content delivery network, optimizing for low latency and high-quality streaming.

    The platform would support various video formats and resolutions, dynamically adjusting based on the viewer's internet connection.

    For content organization and discovery, a robust recommendation engine would be implemented, leveraging machine learning algorithms to analyze user preferences and serve personalized content suggestions. A comprehensive search functionality and categorization system would enhance content discoverability.

    User interaction features would include comments, likes, and share options, fostering community engagement. Additionally, a notification system would keep users informed about new uploads, comments, and recommendations.

    Monetization strategies such as subscription plans, ad-based revenue, or a combination of both could be integrated, ensuring a sustainable business model.

    Security measures would be paramount, encompassing encryption for secure video transmission, protection against content piracy, and measures to prevent unauthorized access. Digital rights management (DRM) would be implemented to control content distribution rights.

    Cross-platform availability is essential, necessitating native applications for various devices and a responsive web interface. Real-time analytics tools would provide insights into user behavior, allowing for continuous improvement and strategic decision-making.

    To handle potential spikes in demand, the server infrastructure should be designed for scalability, with a robust content delivery network and load balancing mechanisms.

    Regular backups and a recovery system would ensure data integrity and availability, even in the event of unforeseen incidents.

    In conclusion, a successful video streaming service would integrate secure user authentication, a powerful content delivery network, personalized recommendation systems, user engagement features, monetization strategies, and robust security measures to deliver a compelling and reliable streaming experience.

    This is a good question to start with. Think about different functions, scalability, performance and resiliency.

    If cannot solve see this solution for detailed discussion.

    My Favorite System Design Problems to Crack Interview

    image_credit --- bytebytego


    2. Design WhatsApp or any other chat system (Solution)
    To design an effective chat system, we would begin with a robust user authentication and authorization mechanism, leveraging secure methods like OAuth to ensure user login integrity.

    For message storage, a scalable and distributed database would be implemented, utilizing indexing for efficient chat history retrieval. Privacy would be a top priority, and thus end-to-end encryption, incorporating strong algorithms and key management systems, would safeguard user messages.

    Real-time communication would be facilitated through WebSocket or similar protocols, with push notifications ensuring timely message alerts. Multimedia support for file sharing, including images and documents, would be integrated with secure storage solutions.

    Group chat functionality would be designed to scale, with features such as member management, admin controls, and real-time synchronization.

    Voice and video calling capabilities would be realized through technologies like WebRTC, complemented by signaling servers for seamless call setup and teardown.

    A user-friendly interface, featuring typing indicators, read receipts, and emoticons, would enhance the overall user experience.

    Cross-platform availability is essential, requiring native applications for iOS and Android, along with a web-based version. A robust notification system, customizable preferences, and status updates would contribute to user engagement.

    Monitoring tools and analytics would be employed for performance tracking and user behavior insights, while backup and recovery mechanisms would ensure data integrity and availability in the face of potential failures.

    In essence, this comprehensive approach would result in a secure, scalable, and feature-rich chat system. It's quite like previous question, think about message distribution, recovery and scalability. If you get stuck see the solution here

    Design WhatsApp or any other chat system (Solution)

    image_credit --- bytebytego


    3. Designing a URL Shortening service like TinyURL (Solution)

    Creating a URL shortening service involves various components to ensure functionality, reliability, and user experience. Initially, a robust user authentication and authorization system would be implemented, employing secure methods such as OAuth for user logins.

    The core functionality would revolve around a scalable database to store original and shortened URLs efficiently, with considerations for indexing and retrieval speed.

    To generate short URLs, a unique algorithm or encoding method would be adopted, ensuring collision-free and concise representations.

    Additionally, implementing user-friendly features such as custom short URLs and URL expiration options could enhance the service's appeal.

    The system would prioritize real-time redirection, utilizing high-speed servers and caching mechanisms to minimize latency. URL analytics could be integrated to provide users with insights into link performance, tracking metrics like clicks, geographical data, and referral sources.

    To handle potential abuse or misuse, a comprehensive security layer would be in place, including measures against malicious links and spam.

    Cross-platform availability is crucial, necessitating a user-friendly web interface and APIs for integration into various applications. A notification system could be implemented to alert users about link activity or analytics updates.

    Considering the potential for heavy traffic, server infrastructure should be designed to scale dynamically. Regular backups and a recovery system would ensure data integrity, allowing for the restoration of data in case of system failures.

    In conclusion, a well-designed URL shortening service would combine secure user authentication, efficient link shortening algorithms, real-time redirection, insightful analytics, and robust security measures to deliver a seamless and reliable user experience.

    And, if you get stuck see the solution given in the link

    Designing a URL Shortening service like TinyURL

    If you need an alternative solution you can also checkout Codemia.io editorial solution for designing TinyURL, its completely free and probably the most complete solution of this problem including functional and non-functional requirement, back of envelop estimations, API design, database design, and much more.


    4. Designing Instagram (solution)

    Designing Instagram involves creating a scalable and feature-rich platform for photo and video sharing. The system would start with a secure user authentication mechanism, using technologies like OAuth for login.

    The core of the system would be a distributed database for storing user profiles, multimedia content, and engagement metrics.

    For media storage, a scalable and efficient solution like cloud storage could be employed, ensuring quick retrieval and low latency.

    Content delivery networks (CDNs) would optimize media distribution for users worldwide. The platform would support image and video formats, with adaptive streaming for different devices and network conditions.

    To enhance user engagement, a recommendation system based on machine learning algorithms could be implemented to provide personalized content suggestions.

    Social features such as likes, comments, and direct messages would be integral, fostering community interaction. Hashtags and a robust search system would improve content discoverability.

    Security measures would include end-to-end encryption for private messages, secure APIs, and content moderation tools to prevent the spread of inappropriate content. Access controls and user privacy settings would be paramount.

    Cross-platform availability is essential, necessitating native applications for iOS and Android, as well as a responsive web interface. Real-time notifications for likes, comments, and new followers would contribute to an engaging user experience.

    Scalability would be ensured through load balancing, caching mechanisms, and a distributed server infrastructure. Regular backups and a recovery system would safeguard against data loss or system failures.

    In conclusion, a successful Instagram system design would integrate secure authentication, scalable media storage, personalized recommendation systems, engaging social features, robust security measures, and a reliable infrastructure for seamless photo and video sharing.

    And, if you get stuck see the solution given in the link

    design instagram


    5. Design a Library Management System (Solution)

    Designing a Library Management System (LMS) involves implementing a user authentication system for library staff, utilizing secure methods like OAuth.

    The core of the system would feature a centralized database to manage books, borrowers, and transactions efficiently.

    The user interface would allow staff to add, update, and delete book records, check in/out books like these system design interview books, and track borrower information. A search functionality and categorization system would enhance book discoverability.

    Security measures would include user access controls and encryption for sensitive data. Automated notifications for overdue books and a reservation system could be incorporated for improved user experience.

    Cross-platform availability could be achieved through a web-based interface. Regular backups and a recovery system would ensure data integrity and if you get stuck see the solution here

    design a library management software


    6. Design a Parking Lot (Solution)

    Designing a parking lot involves creating a system with user authentication for attendants, a centralized database for vehicle records, and an intuitive user interface for managing parking spaces.

    The system would include features like entry/exit logging, real-time space availability updates, and online payment options. Security measures, such as surveillance cameras and access controls, would be implemented.

    Your solution should provide both high level and low level design, including API design and Database or table design.

    A mobile app or website could facilitate user reservations and provide navigation within the parking lot. Regular monitoring and maintenance would ensure optimal functionality, and a backup system would be in place for data recovery in case of system failures.

    And, if you get stuck see the solution here

    design a parking lot solution

    And, if you need another solution of this system design problem then you can also see Codemia.io's editorial solution for designing an efficient Parking Lot . It's one of the most completion solution of this problem including API design, database design, storage and bandwidth requirement, and much more.


    7. How to design Twitter or X app? (solution)

    This is another interesting problem which is often asked in System design interview. You need to design a Twitter or X clone.

    here are the functional requirement for this problem:

    • User can tweet (send) up to 140 character message.
    • User can follow other users.
    • User can like other users' tweets.
    • User's home feed will show tweets from the users they are following.
    • The home feed will show top K popular tweets, based on the number of likes they receive, and the number of followers the author has.

    First try to solve this problem yourself but if you get stuck see the editorial solution of this problem on Codemia.io, one of the best place to practice system design problems.

    Here is the link to solution - Design Twitter Solution
    design Twitter clone


    8. How to design TicketMaster app? (solution)

    The Ticketmaster problem get popular because of Taylor swift ERAS tour concert booking.

    When Taylor swift's this billion dollar tour got popular, reportedly it broke the ticket master platform as millions of Taylor fan's access the site to get their tickets.

    That become history and the problem got popular, but its also a good case study on how to design scalable system which can handle sudden burst of traffic which is hundred or thousand times more than regular traffic.

    Here are functional requirements for this problem:
    User is able to:

    • Search for a theater by city or ZIP code.
    • After selecting a theater, search for movies shown the the theater.
      • After selecting a movie, select which show (e.g. show starting at 1PM, 3PM, 5PM, ...)
      • After selecting a show, user is presented a list of available seats.
      • User can select the seats and finalize the booking.

    Apart from functional, you must pay attention to non-functional requirements like:
    We focus on the following aspects, as they are the most important:

    • Consistency - once a booking is made, it has to be respected. No double bookings.
      • Concurrency - multiple users are interacting with the service to view seats and book seats. Make sure system handles concurrent accesses and provide consistent bookings.
      • Scalability
      • Fault Tolerance Like previous problems, you can first try to solve this on your own but if you get stuck, you can see the detailed solution from Codemia.io where this is a free System design problem and complete, editorial solution is available for free.

    Here is the detailed solution - Design TicketMaster)

    Ticket mater system architecture

    These are a few questions which I always practice before interview. Don't think they are only 6, if you go in-depth it can take days to solve these questions and each one of them will teach you many essential system design concepts and challenges you face.

    All the best with your interviews and don't forget to share what is your favorite System design question.

    By the way, if you are preparing for System design interviews and want to learn System Design in depth then you can also checkout sites like ByteByteGoDesignGuruExponentEducative Codemia.ioBugfree.ai, and Udemy, which have many great System design courses for interviews.

    And here is a nice system design cheat sheet from Exponent to quickly revise essential system design concepts.

    system design cheat sheet

    Other System Design Articles you may like

    Thanks and all the best for your System design interviews.

      Difference between @Controller vs @RestController in Spring Boot

      Hello friends, if you are preparing for Java and Spring Developer interview then you may have come across "difference between @Controller and @RestController annotation" in Spring Boot and Spring Framework in general. It's one of the popular spring question which I have also mentioned in my list of 25 Spring Framework questions earlier