Join Query
A JOIN query is used to combine rows from two or more tables based on a related column between them. There are several types of JOINs in MySQL, including INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.
INNER JOIN
The INNER JOIN returns only the rows that have matching values in both tables.1
2
3
4SELECT columns
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
LEFT JOIN
The LEFT JOIN returns all rows from the left table and the matched rows from the right table. If there is no match, the result is NULL on the right side.1
2
3
4SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;
RIGHT JOIN
The RIGHT JOIN returns all rows from the right table and the matched rows from the left table. If there is no match, the result is NULL on the left side.1
2
3
4SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;
FULL OUTER JOIN
The FULL OUTER JOIN returns all rows when there is a match in either left or right table. If there is no match, the result is NULL on the side that does not have a match.1
2
3
4SELECT columns
FROM table1
FULL OUTER JOIN table2
ON table1.common_column = table2.common_column;
Note: MySQL does not support FULL OUTER JOIN directly, but you can achieve it using a combination of LEFT JOIN and RIGHT JOIN with UNION.1
2
3
4
5
6
7
8
9SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column
UNION
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;
UNION
The UNION operator is used to combine the result sets of two or more SELECT statements. Each SELECT statement within the UNION must have the same number of columns in the result sets with similar data types.1
2
3SELECT column1, column2 FROM table1
UNION
SELECT column1, column2 FROM table2;
SELF JOIN
A SELF JOIN is a regular join but the table is joined with itself. It is useful for querying hierarchical data or comparing rows within the same table.1
2
3
4SELECT a.column1, b.column2
FROM table a
INNER JOIN table a as b
ON a.common_column = b.common_column;
SELECT Query
The SELECT statement is used to query data from a database. It allows you to specify the columns you want to retrieve and the conditions for filtering the data.
Basic SELECT Query
1 | SELECT column1, column2, ... |
Subquery
A subquery is a query nested inside another query. It can be used in various places such as the SELECT, FROM, WHERE, and HAVING clauses to perform operations that require multiple steps.1
2
3SELECT column1, column2
FROM table_name
WHERE column3 IN (SELECT column3 FROM another_table WHERE condition);
1 | SELECT customer_id, customer_name |
1 | SELECT customer_id, customer_name |
1 | -- copy table structure |
Exists
The EXISTS operator is used to test for the existence of any record in a subquery. It returns TRUE if the subquery returns one or more records, and FALSE if it returns no records.1
2
3SELECT column1, column2
FROM table_name
WHERE EXISTS (SELECT column3 FROM another_table WHERE condition);
1 | SELECT customer_id, customer_name |
Common Table Expressions (CTE)
A Common Table Expression (CTE) is a temporary result set that can be referenced within a SELECT, INSERT, UPDATE, or DELETE statement. CTEs are defined using the WITH clause and can improve the readability and maintainability of complex queries.1
2
3
4
5
6
7
8
9WITH cte_name AS (
SELECT column1, column2
FROM table_name
WHERE condition
)
SELECT column1, column2
FROM cte_name
WHERE another_condition;
1 | WITH active_customers AS ( |
Query Cache
MySQL’s query cache is a feature that stores the results of SELECT statements and reuses them when the same query is executed again. This can significantly improve the performance of queries that are executed frequently.
Query Cache Configuration
To enable the query cache, you need to set the query_cache_type parameter to 1 in the my.cnf file. This will enable the query cache for all databases.
1 | [mysqld] |
To enable the query cache for a specific database, you can use the query_cache_type parameter in the CREATE DATABASE statement.1
SELECT SQL_CACHE * FROM table_name WHERE condition; -- Enable query cache for this specific query only works before MySQL 8.0.13






