MySQL
- Introduction To MySQL
- MySQL RDBMS
- MySQL SQL
- MySQL SELECT Statement
- MySQL WHERE Clause
- MySQL AND, OR And NOT Operators
- MySQL ORDER BY Keyword
- MySQL INSERT INTO Statement
- MySQL NULL Values
- MySQL UPDATE Statement
- MySQL DELETE Statement
- MySQL LIMIT Clause
- MySQL MIN() And MAX() Functions
- MySQL COUNT(), AVG() And SUM() Functions
- MySQL LIKE Operator
- MySQL Wildcards
- MySQL IN Operator
- MySQL BETWEEN
- MySQL Aliases
- MySQL Joins
- MySQL INNER JOIN Keyword
- MySQL LEFT JOIN Keyword
- MySQL RIGHT JOIN Keyword
- MySQL CROSS JOIN Keyword
- MySQL Self Join
- MySQL UNION Operator
- MySQL GROUP BY Statement
- MySQL HAVING Clause
- MySQL EXISTS Operator
- MySQL ANY And ALL Operators
- MySQL INSERT INTO SELECT Statement
- MySQL CASE Statement
- MySQL NULL Functions
- MySQL Comments
- MySQL Operators
- MySQL CREATE DATABASE Statement
- MySQL DROP DATABASE Statement
- MySQL CREATE TABLE Statement
- MySQL DROP TABLE Statement
- MySQL ALTER TABLE Statement
- MySQL Constraints
- MySQL NOT NULL Constraint
- MySQL UNIQUE Constraint
- MySQL PRIMARY KEY Constraint
- MySQL FOREIGN KEY Constraint
- MySQL CHECK Constraint
- MySQL DEFAULT Constraint
- MySQL CREATE INDEX Statement
- MySQL AUTO INCREMENT Field
- MySQL Working With Dates
- MySQL Views
- MySQL Data Types
- MySQL Functions
MySQL Self Join
MySQL Self Join
A self join is a regular join, but the table is joined with itself.
Self Join Syntax
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
FROM table1 T1, table1 T2
WHERE condition;
T1 and T2 are different table aliases for the same table.
Practice Excercise Practice now
Demo Database
In this tutorial we will use the well-known Northwind sample database.
Below is a selection from the "Customers" table:
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
1 |
Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
Practice Excercise Practice now
MySQL Self Join Example
The following SQL statement matches customers that are from the same city:
SELECT A.CustomerName AS CustomerName1, B.CustomerName AS CustomerName2, A.City
FROM Customers A, Customers B
WHERE A.CustomerID <> B.CustomerID
AND A.City = B.City
ORDER BY A.City;
FROM Customers A, Customers B
WHERE A.CustomerID <> B.CustomerID
AND A.City = B.City
ORDER BY A.City;
Practice Excercise Practice now
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP