Chapter-15 : Self Join & Table Alias in SQL with example | MySQL database

Published: 03 November 2023
on channel: All about data
147
4

Self Join & Table Alias in SQL with example | MySQL database

📺 YouTube :    / @all_about_data_  
📷 Instagram :   / allabout_da.  .
📺Google Drive : https://drive.google.com/drive/folder...


Self join in SQL is a join that is performed between a table and itself. This can be useful for tasks such as finding hierarchical data or comparing rows within the same table.

To perform a self join in SQL, you use the JOIN or INNER JOIN clause and specify the table name twice. You can also use table aliases to make the query more readable.


Self join in SQL is a join that is performed between a table and itself. This can be useful for tasks such as finding hierarchical data or comparing rows within the same table.

To perform a self join in SQL, you use the JOIN or INNER JOIN clause and specify the table name twice. You can also use table aliases to make the query more readable.

For example, the following query will find all customers who have placed an order with themselves:

SQL
SELECT c1.customer_id, c1.name, c2.name AS ordered_from
FROM customers c1
INNER JOIN customers c2 ON c1.customer_id = c2.ordered_by_customer_id;
Use code with caution. Learn more
This query will return all rows from the customers table where the customer_id column is equal to the ordered_by_customer_id column.




Table aliases in SQL are used to give a table a different name for the purpose of a query. This can be useful when you are joining two tables with the same name or when you are trying to make your query more readable.

To create a table alias, you use the AS keyword. For example, the following query will join the customers table to itself and use the aliases c1 and c2:


On this page of the site you can watch the video online Chapter-15 : Self Join & Table Alias in SQL with example | MySQL database with a duration of hours minute second in good quality, which was uploaded by the user All about data 03 November 2023, share the link with friends and acquaintances, this video has already been watched 147 times on youtube and it was liked by 4 viewers. Enjoy your viewing!