SQL Questions ::
Soln :: Self Join
A self join is a join in which a table is joined with itself (which is also called Unary relationships), specially when the table has a FOREIGN KEY which references its own PRIMARY KEY. To join a table itself means that each row of the table is combined with itself and with every other row of the table.
The self join can be viewed as a join of two copies of the same table. The table is not actually copied, but SQL performs the command as though it were.
The syntax of the command for joining a table to itself is almost same as that for joining two different tables. To distinguish the column names from one another, aliases for the actual the table name are used, since both the tables have same name. Table name aliases are defined in the FROM clause of the SELECT statement
SELECT a.column_name, b.column_name... FROM table1 a, table1 b WHERE a.common_filed = b.common_field;
CREATE TABLE COMPANY(ID INT NOT NULL, NAME TEXT NOT NULL, MANAGER_ID INT NOT NULL);
INSERT INTO COMPANY (ID,NAME,MANAGER_ID) VALUES (1, 'Suresh', 5);
INSERT INTO COMPANY (ID,NAME,MANAGER_ID) VALUES (2, 'Ramu', 5);
INSERT INTO COMPANY (ID,NAME,MANAGER_ID) VALUES (3, 'SureshAV', 4);
INSERT INTO COMPANY (ID,NAME,MANAGER_ID) VALUES (4, 'Kalidoss', 6);
INSERT INTO COMPANY (ID,NAME,MANAGER_ID) VALUES (5, 'Ramesh', 6);
INSERT INTO COMPANY (ID,NAME,MANAGER_ID) VALUES (6, 'Ramki', 7);
select b.NAME, b.ID, a.NAME as MANAGER_NAME from COMPANY a, COMPANY b where a.ID = b.MANAGER_ID;
Soln :: Self Join
A self join is a join in which a table is joined with itself (which is also called Unary relationships), specially when the table has a FOREIGN KEY which references its own PRIMARY KEY. To join a table itself means that each row of the table is combined with itself and with every other row of the table.
The self join can be viewed as a join of two copies of the same table. The table is not actually copied, but SQL performs the command as though it were.
The syntax of the command for joining a table to itself is almost same as that for joining two different tables. To distinguish the column names from one another, aliases for the actual the table name are used, since both the tables have same name. Table name aliases are defined in the FROM clause of the SELECT statement
self
join is a join in which a table is joined with itself (which is also
called Unary relationships), specially when the table has a FOREIGN KEY
which references its own PRIMARY KEY. To join a table itself means
that each row of the table is combined with itself and with every other
row of the table.
The self join can be viewed as a join of two copies of the same table. The table is not actually copied, but SQL performs the command as though it were.
The syntax of the command for joining a table to itself is almost same as that for joining two different tables. To distinguish the column names from one another, aliases for the actual the table name are used, since both the tables have same name. Table name aliases are defined in the FROM clause of the SELECT statement. See the syntax :
- See more at: http://www.w3resource.com/sql/joins/perform-a-self-join.php#sthash.xWmie4wh.dpuf
The self join can be viewed as a join of two copies of the same table. The table is not actually copied, but SQL performs the command as though it were.
The syntax of the command for joining a table to itself is almost same as that for joining two different tables. To distinguish the column names from one another, aliases for the actual the table name are used, since both the tables have same name. Table name aliases are defined in the FROM clause of the SELECT statement. See the syntax :
- See more at: http://www.w3resource.com/sql/joins/perform-a-self-join.php#sthash.xWmie4wh.dpuf
SELECT a.column_name, b.column_name... FROM table1 a, table1 b WHERE a.common_filed = b.common_field;
self
join is a join in which a table is joined with itself (which is also
called Unary relationships), specially when the table has a FOREIGN KEY
which references its own PRIMARY KEY. To join a table itself means
that each row of the table is combined with itself and with every other
row of the table.
The self join can be viewed as a join of two copies of the same table. The table is not actually copied, but SQL performs the command as though it were.
The syntax of the command for joining a table to itself is almost same as that for joining two different tables. To distinguish the column names from one another, aliases for the actual the table name are used, since both the tables have same name. Table name aliases are defined in the FROM clause of the SELECT statement. See the syntax :
- See more at: http://www.w3resource.com/sql/joins/perform-a-self-join.php#sthash.xWmie4wh.dpuf
The self join can be viewed as a join of two copies of the same table. The table is not actually copied, but SQL performs the command as though it were.
The syntax of the command for joining a table to itself is almost same as that for joining two different tables. To distinguish the column names from one another, aliases for the actual the table name are used, since both the tables have same name. Table name aliases are defined in the FROM clause of the SELECT statement. See the syntax :
- See more at: http://www.w3resource.com/sql/joins/perform-a-self-join.php#sthash.xWmie4wh.dpuf
self
join is a join in which a table is joined with itself (which is also
called Unary relationships), specially when the table has a FOREIGN KEY
which references its own PRIMARY KEY. To join a table itself means
that each row of the table is combined with itself and with every other
row of the table.
The self join can be viewed as a join of two copies of the same table. The table is not actually copied, but SQL performs the command as though it were.
The syntax of the command for joining a table to itself is almost same as that for joining two different tables. To distinguish the column names from one another, aliases for the actual the table name are used, since both the tables have same name. Table name aliases are defined in the FROM clause of the SELECT statement. See the syntax :
- See more at: http://www.w3resource.com/sql/joins/perform-a-self-join.php#sthash.xWmie4wh.dpuf
The self join can be viewed as a join of two copies of the same table. The table is not actually copied, but SQL performs the command as though it were.
The syntax of the command for joining a table to itself is almost same as that for joining two different tables. To distinguish the column names from one another, aliases for the actual the table name are used, since both the tables have same name. Table name aliases are defined in the FROM clause of the SELECT statement. See the syntax :
- See more at: http://www.w3resource.com/sql/joins/perform-a-self-join.php#sthash.xWmie4wh.dpuf
self
join is a join in which a table is joined with itself (which is also
called Unary relationships), specially when the table has a FOREIGN KEY
which references its own PRIMARY KEY. To join a table itself means
that each row of the table is combined with itself and with every other
row of the table.
The self join can be viewed as a join of two copies of the same table. The table is not actually copied, but SQL performs the command as though it were.
The syntax of the command for joining a table to itself is almost same as that for joining two different tables. To distinguish the column names from one another, aliases for the actual the table name are used, since both the tables have same name. Table name aliases are defined in the FROM clause of the SELECT statement. See the syntax :
- See more at: http://www.w3resource.com/sql/joins/perform-a-self-join.php#sthash.xWmie4wh.dpuf
EXAMPLE::The self join can be viewed as a join of two copies of the same table. The table is not actually copied, but SQL performs the command as though it were.
The syntax of the command for joining a table to itself is almost same as that for joining two different tables. To distinguish the column names from one another, aliases for the actual the table name are used, since both the tables have same name. Table name aliases are defined in the FROM clause of the SELECT statement. See the syntax :
- See more at: http://www.w3resource.com/sql/joins/perform-a-self-join.php#sthash.xWmie4wh.dpuf
CREATE TABLE COMPANY(ID INT NOT NULL, NAME TEXT NOT NULL, MANAGER_ID INT NOT NULL);
INSERT INTO COMPANY (ID,NAME,MANAGER_ID) VALUES (1, 'Suresh', 5);
INSERT INTO COMPANY (ID,NAME,MANAGER_ID) VALUES (2, 'Ramu', 5);
INSERT INTO COMPANY (ID,NAME,MANAGER_ID) VALUES (3, 'SureshAV', 4);
INSERT INTO COMPANY (ID,NAME,MANAGER_ID) VALUES (4, 'Kalidoss', 6);
INSERT INTO COMPANY (ID,NAME,MANAGER_ID) VALUES (5, 'Ramesh', 6);
INSERT INTO COMPANY (ID,NAME,MANAGER_ID) VALUES (6, 'Ramki', 7);
select b.NAME, b.ID, a.NAME as MANAGER_NAME from COMPANY a, COMPANY b where a.ID = b.MANAGER_ID;
No comments:
Post a Comment