Databases are used to store data in tables. A database may have multiple tables, with common columns. Joins in SQL are used to combine these tables, more like stitching together pieces of related data from different tables.
Consider a company’s database with the following tables: Employees, Departments and Projects. We will use this to discuss the types of joins.
The Employees table has the following columns:

The departments table has the following columns:
The Projects table has the following columns:
INNER JOIN
Returns the rows that match in both tables.
LEFT JOIN
Returns all rows from the left table and the matching rows from the right table.
RIGHT JOIN
Returns all rows from the right table and the matching rows from the left table.
FULL OUTER JOIN
Combines the left and right join, then shows all rows from the tables. If there are no matches, it uses NULLS.
CROSS JOIN
Returns a combination of rows from both tables.
SELF JOIN
Joins a table to itself.
SQL WINDOW FUNCTIONS
Allows you to perform mathematical calculations across a set of rows.
Consider an Sales database with the following tables: Customers, Orders and Books. We will use this to discuss the window functions.
1. ROW_NUMBER() - Assigns a unique integer to rows starting from 1 based on the order specified by the ORDER BY clause.
2. RANK() - Assign the same rank when there are ties and skips. It leaves a gap.
3. DENSE_RANK() - Assigns ranks, but does not leave a gap in the ranking.
4. LEAD()- Access data from the next row. It is used to track trends.
5. LAG() - Access data from the previous row.
6. NTILE()- Divide the result set into a specified number of roughly equal parts.














Top comments (0)