There are lots of articles out there teaching you how to write queries. Knowing how to write a single query is not really the hard part. The hard part is that there are always multiple options of approaches and all of them require a combination of queries. To put questions into categories, it helps us to identify patterns and build better instincts on what kinds of queries we can use.
There are basically 3 types of SQL questions. The three types of questions are very simple in their original form. However, they can be leveled up by mix and match with 3 things. They are Time Constraints, Calculation Requirments, and Comparison/Ranking Requirments. I will show you what I meant with examples. Let’s clarify the most basic forms first.
Type 1: Select all
Type 2: Select the group that did X
Type 3: Select the group that didn’t do X
As you will see, Type-1 questions are basically testing our ability to use different kinds of joins. Both Type-2 and Type-3 questions are testing our ability to precisely identify the special group that satisfies certain constraints.
It is important to draw the distinction between Type 2 and Type 3. Type 2 requires only 1 step with the where statement. Type 3 requires a 2-steps approach. Firstly, we need to identify the special group with **where **statement. Secondly, we need to use not in to exclude that group in the **where **statement.
The reason is that if we directly select everything that is not green, then the samples that are both red and green will still be selected. I will provide examples below and it will become clearer. As you will see, Type 3 can become a lot more complicated than Type 2 questions.
#study-summary #sql #sql-queries #leetcode #code-interview