Understanding Cascade Left Joins and Writing Complex Queries
In SQL, the left join is a powerful tool for combining data from multiple tables based on a common column. In this blog post, we will explore the concept of cascade left joins, providing clear explanations and examples to help you grasp this important technique. Additionally, we will delve into writing complex queries, enabling you to tackle more advanced data retrieval tasks with confidence.
a left join b left join c
The basic each join knowledge, pelase refer https://zhuanlan.zhihu.com/p/29234064
let’s give an example:
- table
total
: total student in one school, includes two columns student id and room id, namedid
androom
. - table
active
: active student who go to library in the past 30 days, also includes same two columns. - table
paid
: paid student who paid for library to get static seat in library in the past 30 days, also includes same two columns.
Create table:
1 | CREATE TABLE IF NOT EXISTS `total` ( |
So what’s the output of a left join b left join c ? Before get answer, please understand:
A LEFT JOIN B
: This indicates that table A is the left table, and table B is the right table. The left join between A and B returns all rows from table A, along with any matching rows from table B. If there is no match, the columns from table B will contain NULL values.A LEFT JOIN B LEFT JOIN C
: This extends the previous left join to include table C. In this case, the left join between A and B is performed first. Then, the result of that join is left joined with table C. This means that all rows from table A are preserved, along with any matching rows from table B and C. Again, if there is no match, the columns from the respective tables will contain NULL values.
let’s see, http://sqlfiddle.com/#!9/5911603/14/0
1 | select |
if changed to left join paid on active.id = paid.id
to left join paid on total.id = paid.id
, will base on total.id not active.id
user case
Let’s implement this case, want to know how many student count per room with different student type
inactive_student
: how many student is inactive student to use library, should betotal left join active on total.id = active.id where active.id is NULL
paid_active_student
: how many student is paid active student to use library, should beactive left join paid on active.id = paid.id where paid.id is not NULL
not_paid_active_student
: how many user is not paid active student to use library, should beactive left join paid on active.id = paid.id where paid.id is NULL
How to achieve above case by one sql query, let’s understand step by step.
sql: http://sqlfiddle.com/#!9/5911603/3/0
IMPORTANT: the conditions within the CASE WHEN
statement are evaluated in order
, and once a condition evaluates to true, the corresponding result is returned, and the subsequent conditions are not evaluated.
1 | select |
Then based on above result to group by to know per room student type.
sql: [http://sqlfiddle.com/#!9/5911603/13/0](http://sqlfiddle.com/#!9/5911603/13/0
1 | select |
sql query execution order
for better understand above use case, let’s talk about sql query execution order. If you are familar with it, please skip.
In SQL, the order of execution of a query is generally as follows:
FROM
clause: This specifies the tables or views involved in the query and sets up the initial result set.
JOIN
clause: If there are any join operations specified in the query, the join conditions are evaluated, and the appropriate rows are combined from the joined tables.WHERE
clause: This filters the rows from the result set based on the specified conditions.GROUP BY
clause: If grouping is specified, the result set is divided into groups based on the specified grouping columns.HAVING
clause: This filters the groups from the result set based on the specified conditions.SELECT
clause: This selects the desired columns from the result set.DISTINCT
keyword: If present, duplicate rows are eliminated from the result set.ORDER BY
clause: The result set is sorted based on the specified columns and sort order.LIMIT
orOFFSET
clauses: If specified, the result set is limited to a certain number of rows or skipped by a certain number of rows.