Any SQL query is not fundamentally related to performance, but when used in large fields and its amount is expected to be very large, the performance issue is always present. To resolve these issues, the only possible solution is that the query must determine what it is doing and how much time it is consuming to do that task. And when any data analysts know about it, they may be able to optimize that query a lot.
In SQL, the EXPLAIN keyword provides a description of how the SQL queries are executed by the databases. These descriptions include the optimizer logs, how tables are joined and in what order, etc.
Hence, it would be a useful tool in query optimization and knowing the details of its execution step by step. The EXPLAIN also indicates the fact that a user who doesn’t have any access to a particular database will not be provided details about how it executes the queries. So it maintains security as well.
The primary thing to note about EXPLAIN is that it will be used at the beginning of the query, i.e., before SELECT, INSERT, UPDATE, etc.
Syntax:
EXPLAIN (QUERY Statement) ;
/* ONLY TAKES COMMAND AS PARAMETER */
Example:
Let’s first create a database known as GFG using the below command:
CREATE DATABASE GFG;
Now add tables to it.
SELECT * FROM gfgtable;
We added some data to it, and now it looks as below:
Now let’s use EXPLAIN to get an explanation of any query beginning from a simpler one.
QUERY1: EXPLAIN SELECT * FROM gfgtable;

All details of the execution is given under some specified column names.
Columns in the output of EXPLAIN Keyword:
Explain keyword results in their output using some column names as shown above. They are explained below:
- id: It represents the id of the query that is to be explained.
- SELECT_TYPE: The complexity of the select clause is shown here. In the above case, it is very simple.
- table: The name of the table used is displayed here.
- Partitions: This shows the number of partitions in the table joined in the query.
- type: It specifies the join type.
- possible_keys: Which keys could have been used?
- key: which keys are used?
- key_len: Length of the key used.
- ref: Mentions any sort of reference used in the query while comparing columns or not.
- rows: The number of rows over which the query acts.
- Filtered: The rows that are filtered using the conditions in the WHERE clause.
- Extra: Some additional details regarding the executed query.
In this way, EXPLAIN keyword is used to get all the information about the query and tabulate them so that it can be stored in DB for further use.
QUERY 2:
EXPLAIN
SELECT CONCAT(g.FirstName,g.LastName)
AS FULLNAME
FROM gfgtable AS g, gfgtable AS h
WHERE g.Roll=h.Roll;
Output:

Here we can see how the type of join is also mentioned. SHOW WARNINGS are also used with the EXPLAIN keyword to display the details of the last executed line from where the error originated in the query.
Similar Reads
SQL Engine
The SQL Query Execution Engine is a critical component of any Database Management System (DBMS). It plays a key role in processing SQL queries and transforming them into meaningful results. Every time a user runs a query to fetch data from a database, various complex processes occur simultaneously t
7 min read
IN vs EXISTS in SQL
SQL stands for Structured Query Language. SQL is used for retrieving useful information from a large set of data and it is used for storing the data in the Database, modifying, or manipulating the data from the database. In this article, we are going to discuss IN Operator and EXISTS Operator in SQL
5 min read
CTE in SQL
In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
SQL Inner Join
SQL INNER JOIN is a powerful and frequently used operation in relational databases. It allows us to combine two or more tables based on a related column, returning only the records that satisfy the join condition This article will explore the fundamentals of INNER JOIN, its syntax, practical example
4 min read
MySQL EXPLAIN ANALYZE
MySQL's EXPLAIN ANALYZE command provides detailed information about query execution plans, including specifics about the optimizer's decisions, access methods, and estimated costs. Unlike the EXPLAIN statement, which outlines the execution plan without running the query, EXPLAIN ANALYZE actually exe
3 min read
How to Read MySQL EXPLAIN?
Understanding how to interpret and analyze the output of the MySQL EXPLAIN statement is essential for database administrators, developers, and anyone involved in optimizing database performance. The EXPLAIN statement provides insights into how MySQL executes queries and helps identify potential bott
4 min read
Boolean Expressions in SQL
Boolean expressions are a core concept in SQL, helping to filter and manipulate data based on conditions. These expressions evaluate to one of three Boolean values: TRUE, FALSE, or UNKNOWN. They are extensively used in WHERE clauses, HAVING clauses, and conditional statements to query and retrieve s
3 min read
SQL Injection
SQL Injection is a security flaw in web applications where attackers insert harmful SQL code through user inputs. This can allow them to access sensitive data, change database contents or even take control of the system. It's important to know about SQL Injection to keep web applications secure. In
7 min read
SQL FULL JOIN
In SQL, the FULL JOIN (or FULL OUTER JOIN) is a powerful technique used to combine records from two or more tables. Unlike an INNER JOIN, which only returns rows where there are matches in both tables, a FULL JOIN retrieves all rows from both tables, filling in NULL values where matches do not exist
4 min read
SQL for Machine Learning
Integrating SQL with machine learning can provide a powerful framework for managing and analyzing data, especially in scenarios where large datasets are involved. By combining the structured querying capabilities of SQL with the analytical and predictive capabilities of machine learning algorithms,
6 min read