Skip to the article

I'm Fernando,
a full-stack developer!

How SQL selects execution in the background

SQL queries are typically seen as something magical, but understanding how they get executed behind the scenes helps you in debugging them. SQL engines typically apply a lot of optimizations in the background to speed them up, but understanding the simple model still helps you to know why something happens and even allows you to build your own SQL like engines

The SQL engine follows the following steps:

  • Collect rows based on from and joins
  • Filter the rows based on where and on clauses
  • Group data via grouping functions
  • Filter the rows based on having clauses
  • Order the results
  • Skip N rows and then emit N rows
  • Run select expressions

Lets take a basic table structure

Course:

idnameteacher_id
1Learning SQL1
2Learning Coding1
3History1
4Learning history2

Teacher:

idname
1Alice
2Bob
3Carol
select teacher.name, coalesce(string_agg(lesson.name, ', '), 'no courses planned')
from teacher
left join course on teacher.id = course.teacher_id
where lesson.name like 'Learning%'
group by teacher.name
order by count(lesson.id)

Internally, the blocks of the above query become:

Select list:

  • teacher.name
  • coalesce(<result #1>, ', ')

Tables:

  • courses
  • teacher (with filter teacher.id = course.teacher_id)

Where:

  • lesson.name like 'Learning%

Grouping by:

  • teacher.name

Grouping expressions:

  • string_agg(lesson.name, ', ')
  • count(lesson.id)

Following our initial steps, we start by joining all tables together.

We start with teachers

idname
1Alice
2Bob
3Carol

We now add the teacher table to this with the above filter. A left join can be seen as the following loop:

const output = [];
for(const leftRow of left) {
  let matched = false;
  for (const rightRow of right) {
    if (matches(leftRow, rightRow)) {
      matched = true;
      output.push({
        leftRow,
        rightRow
      })
    }
  }
  if(!matches) {
    output.push({
      leftRow,
      rightRow: null
    })
  }
}
courses.idcourses.namecourses.teacher_idteacher.idteacher.name
1Learning SQL11Alice
2Learning Coding11Alice
3History11Alice
4Learning history22Bob
nullnullnull3Carol

Comments