0
0
PostgreSQLquery~5 mins

Self join patterns in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a self join in SQL?
A self join is when a table is joined with itself to compare rows within the same table.
Click to reveal answer
beginner
Why use table aliases in a self join?
Aliases give different names to the same table so you can refer to it twice in the query clearly.
Click to reveal answer
intermediate
Example: How to find employees who have the same manager using self join?
Use a self join on the employee table matching manager_id to employee_id with aliases for clarity.
Click to reveal answer
intermediate
What is the difference between INNER JOIN and LEFT JOIN in self join patterns?
INNER JOIN returns only matching rows between the table and itself, LEFT JOIN returns all rows from the left side even if no match.
Click to reveal answer
advanced
How can self joins help in hierarchical data queries?
Self joins let you compare parent and child rows in the same table to explore hierarchy levels.
Click to reveal answer
What keyword is essential to distinguish the same table twice in a self join?
AHAVING
BGROUP BY
CDISTINCT
DAlias
In a self join, if you want all rows from the left table even if no match exists on the right, which join do you use?
ALEFT JOIN
BRIGHT JOIN
CINNER JOIN
DFULL JOIN
Which of these is a common use case for self joins?
AJoining two different tables
BComparing rows within the same table
CFiltering rows with WHERE
DCreating indexes
What does the ON clause specify in a self join?
AThe grouping of rows
BThe order of rows
CHow to match rows between the two aliases of the same table
DThe columns to select
If you want to find employees who share the same manager using a self join, which columns would you join on?
Aemployee.manager_id = manager.employee_id
Bemployee.employee_id = manager.employee_id
Cemployee.name = manager.name
Demployee.salary = manager.salary
Explain what a self join is and why table aliases are important in it.
Think about how you can use the same table twice in one query.
You got /3 concepts.
    Describe a real-life example where a self join can be used to find relationships within the same data set.
    Consider a company employee list with managers.
    You got /3 concepts.