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

Pubblicato il: 03 novembre 2023
sul canale di: 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:


In questa pagina del sito puoi guardare il video online Chapter-15 : Self Join & Table Alias in SQL with example | MySQL database della durata di ore minuti seconda in buona qualità , che l'utente ha caricato All about data 03 novembre 2023, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 147 volte e gli è piaciuto 4 spettatori. Buona visione!