Computer-Science

  1. Basic SQL
  2. Practice

Basic SQL

SQL์˜ ์ œ์•ฝ ์กฐ๊ฑด ์ง€์ •

NOT NULL, DEFAULT

์†์„ฑ์—์„œ์˜ NOT NULL ์ œ์•ฝ ์กฐ๊ฑด์€ ๊ทธ ์†์„ฑ์— ๋Œ€ํ•ด NULL ๊ฐ’์ด ํ—ˆ๋ฝ๋˜์ง€ ์•Š์Œ์„ ๋ช…์‹œํ•œ๋‹ค. ๋‹ฌ๋ฆฌ ๋งํ•˜๋ฉด, ์ œ์•ฝ์กฐ๊ฑด์€ ๊ทธ ์†์„ฑ์˜ ๋„๋ฉ”์ธ์—์„œ NULL ๊ฐ’์„ ์ œ์™ธ์‹œํ‚จ๋‹ค.

SQL์—์„œ์˜ ๊ธฐ๋ณธ ๊ฒ€์ƒ‰ ์งˆ์˜

SQL ํ‘œํ˜„์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋Š” SELECT, FROM, WHERE์˜ ์„ธ ๊ฐœ์˜ ์ ˆ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค. ์งˆ์˜๋Š” FROM ์ ˆ์— ๋‚˜์—ด๋œ ๋ฆด๋ ˆ์ด์…˜๋“ค์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๊ณ , WHERE๊ณผ SELECT ์ ˆ์— ๋ช…์‹œ๋œ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•ด ๊ฒฐ๊ณผ๋กœ ๋ฆด๋ ˆ์ด์…˜์„ ๋งŒ๋“ค์–ด ๋‚ธ๋‹ค.

JOIN์˜ ์ข…๋ฅ˜

JOIN (์กฐ์ธ)์ด๋ž€?

๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์ด๋‚˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ํ•˜๋‚˜๋กœ ๋งŒ๋“ค๊ณ , ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•

  1. Inner Join
  2. Outer Join
  3. Cross Join
  4. Self Join
  5. Anti Join
  6. Semi Join

Inner Join

SELECT <select_list>
FROM Table_A A
INNER JOIN Table_B B
ON A.Key = B.Key

Equi Join

Natural Join

Outer Join

Outer Join์ด๋ž€ ์กฐ์ธ ์กฐ๊ฑด์—์„œ ๋™์ผํ•œ ๊ฐ’์ด ์—†๋Š” ํ–‰๋„ ๋ฐ˜ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

Left Outer Join

SELECT <select_list>
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key

Right Outer Join

SELECT <select_list>
FROM Table_A A
RIGHT JOIN Table_B B
ON A.Key = B.Key

Full Outer Join

SELECT <select_list>
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.Key = B.Key

Cross Join

SELECT
A.NAME, B.AGE
FROM TABLE_A A, TABLE_A B

Self Join

Anti Join

SELECT *
FROM employee a, department b WHERE a.dno = b.dnumber AND a.dno
NOT IN
      ( SELECT dnumber
      FROM department
      WHERE mgr_ssn IS NULL) ;

Semi Join

SELECT *
FROM employee a, department b WHERE a.dno = b.dnumber AND a.dno
IN
 ( SELECT dnumber
   FROM department
   WHERE mgr_ssn IS NOT NULL ) ;

SQL์—์„œ์˜ ์ง‘ํ•ฉ ์—ฐ์‚ฐ

๋ฌธ์ž์—ด์— ๋Œ€ํ•œ ๋น„๊ต

์งˆ์˜ ๊ฒฐ๊ณผ์˜ ์ •๋ ฌ

SQL์—์„œ์˜ INSERT, DELETE, UPDATE ๊ตฌ๋ฌธ

INSERT INTO VALUE

SELECT์™€ ๊ฒฐํ•ฉ๋œ INSERT ๋ช…๋ น

CREATE TABLE WORKS_ON_INFO
(  EMP_NAME       VARCHAR(15),
   PROJ_NAME      VARCHAR(15),
   HOURS_PER_WEEK DECIMAL(3,1) );
INSERT INTO WORKS_ON_INFO (EMP_NAME, PROJ_NAME, HOURS_PER_WEEK)
SELECT E.LNAME, P.PNAME, W.HOURS
FROM PROJECT P, WORKS_ON W, EMPLOYEE E
WHERE P.PNUMBER=W.PNO AND W.ESSN=E.SSN;

DELETE ๋ฌธ

UPDATE ๋ฌธ


Basic SQL Practice

Company Database

Go to Problems

mysql> use company;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_company |
+-------------------+
| DEPARTMENT        |
| DEPENDENT         |
| DEPT_LOCATIONS    |
| EMPLOYEE          |
| PROJECT           |
| WORKS_ON          |
+-------------------+
6 rows in set (0.00 sec)

mysql> desc department;
+----------------+-------------+------+-----+---------+-------+
| Field          | Type        | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| Dname          | varchar(15) | NO   | UNI | NULL    |       |
| Dnumber        | int         | NO   | PRI | NULL    |       |
| Mgr_ssn        | char(9)     | NO   | MUL | NULL    |       |
| Mgr_start_date | date        | YES  |     | NULL    |       |
+----------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> desc dependent;
+----------------+-------------+------+-----+---------+-------+
| Field          | Type        | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| Essn           | char(9)     | NO   | PRI | NULL    |       |
| Dependent_name | varchar(15) | NO   | PRI | NULL    |       |
| Sex            | char(1)     | YES  |     | NULL    |       |
| Bdate          | date        | YES  |     | NULL    |       |
| Relationship   | varchar(8)  | YES  |     | NULL    |       |
+----------------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> desc dept_locations;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| Dnumber   | int         | NO   | PRI | NULL    |       |
| Dlocation | varchar(15) | NO   | PRI | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> desc employee;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| Fname     | varchar(10)  | NO   |     | NULL    |       |
| Minit     | char(1)      | YES  |     | NULL    |       |
| Lname     | varchar(20)  | NO   |     | NULL    |       |
| Ssn       | char(9)      | NO   | PRI | NULL    |       |
| Bdate     | date         | YES  |     | NULL    |       |
| Address   | varchar(30)  | YES  |     | NULL    |       |
| Sex       | char(1)      | YES  |     | NULL    |       |
| Salary    | decimal(5,0) | YES  |     | NULL    |       |
| Super_ssn | char(9)      | YES  | MUL | NULL    |       |
| Dno       | int          | NO   | MUL | NULL    |       |
+-----------+--------------+------+-----+---------+-------+
10 rows in set (0.00 sec)

mysql> desc project;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| Pname     | varchar(15) | NO   | UNI | NULL    |       |
| Pnumber   | int         | NO   | PRI | NULL    |       |
| Plocation | varchar(15) | YES  |     | NULL    |       |
| Dnum      | int         | NO   | MUL | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> desc works_on;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| Essn  | char(9)      | NO   | PRI | NULL    |       |
| Pno   | int          | NO   | PRI | NULL    |       |
| Hours | decimal(3,1) | NO   |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> select * from department;
+----------------+---------+-----------+----------------+
| Dname          | Dnumber | Mgr_ssn   | Mgr_start_date |
+----------------+---------+-----------+----------------+
| Headquarters   |       1 | 888665555 | 1981-06-19     |
| Administration |       4 | 987654321 | 1995-01-01     |
| Research       |       5 | 333445555 | 1988-05-22     |
+----------------+---------+-----------+----------------+
3 rows in set (0.01 sec)

mysql> select * from dependent;
+-----------+----------------+------+------------+--------------+
| Essn      | Dependent_name | Sex  | Bdate      | Relationship |
+-----------+----------------+------+------------+--------------+
| 123456789 | Alice          | F    | 1988-12-30 | Daughter     |
| 123456789 | Elizabeth      | F    | 1967-05-05 | Spouse       |
| 123456789 | Michael        | M    | 1988-01-04 | Son          |
| 333445555 | Alice          | F    | 1986-04-04 | Daughter     |
| 333445555 | Joy            | F    | 1958-05-03 | Spouse       |
| 333445555 | Theodore       | M    | 1983-10-25 | Son          |
| 987654321 | Abner          | M    | 1942-02-28 | Spouse       |
+-----------+----------------+------+------------+--------------+
7 rows in set (0.00 sec)

mysql> select * from dept_locations;
+---------+-----------+
| Dnumber | Dlocation |
+---------+-----------+
|       1 | Houston   |
|       4 | Stafford  |
|       5 | Bellaire  |
|       5 | Houston   |
|       5 | Sugarland |
+---------+-----------+
5 rows in set (0.00 sec)

mysql> select * from employee;
+----------+-------+---------+-----------+------------+-------------------------+------+--------+-----------+-----+
| Fname    | Minit | Lname   | Ssn       | Bdate      | Address                 | Sex  | Salary | Super_ssn | Dno |
+----------+-------+---------+-----------+------------+-------------------------+------+--------+-----------+-----+
| John     | B     | Smith   | 123456789 | 1965-01-09 | 731 Fondren, Houston TX | M    |  30000 | 333445555 |   5 |
| Franklin | T     | Wong    | 333445555 | 1965-12-08 | 638 Voss, Houston TX    | M    |  40000 | 888665555 |   5 |
| Joyce    | A     | English | 453453453 | 1972-07-31 | 5631 Rice, Houston TX   | F    |  25000 | 333445555 |   5 |
| Ramesh   | K     | Narayan | 666884444 | 1962-09-15 | 975 Fire Oak, Humble TX | M    |  38000 | 333445555 |   5 |
| James    | E     | Borg    | 888665555 | 1937-11-10 | 450 Stone, Houston TX   | M    |  55000 | NULL      |   1 |
| Jennifer | S     | Wallace | 987654321 | 1941-06-20 | 291 Berry, Bellaire TX  | F    |  43000 | 888665555 |   4 |
| Ahmad    | V     | Jabbar  | 987987987 | 1969-03-29 | 980 Dallas, Houston TX  | M    |  25000 | 987654321 |   4 |
| Alicia   | J     | Zelaya  | 999887777 | 1968-01-19 | 3321 Castle, Spring TX  | F    |  25000 | 987654321 |   4 |
+----------+-------+---------+-----------+------------+-------------------------+------+--------+-----------+-----+
8 rows in set (0.01 sec)

mysql> select * from project;
+-----------------+---------+-----------+------+
| Pname           | Pnumber | Plocation | Dnum |
+-----------------+---------+-----------+------+
| ProductX        |       1 | Bellaire  |    5 |
| ProductY        |       2 | Sugarland |    5 |
| ProductZ        |       3 | Houston   |    5 |
| Computerization |      10 | Stafford  |    4 |
| Reorganization  |      20 | Houston   |    1 |
| Newbenefits     |      30 | Stafford  |    4 |
+-----------------+---------+-----------+------+
6 rows in set (0.00 sec)

mysql> select * from works_on;
+-----------+-----+-------+
| Essn      | Pno | Hours |
+-----------+-----+-------+
| 123456789 |   1 |  32.5 |
| 123456789 |   2 |   7.5 |
| 333445555 |   2 |  10.0 |
| 333445555 |   3 |  10.0 |
| 333445555 |  10 |  10.0 |
| 333445555 |  20 |  10.0 |
| 453453453 |   1 |  20.0 |
| 453453453 |   2 |  20.0 |
| 666884444 |   3 |  40.0 |
| 888665555 |  20 |  16.0 |
| 987654321 |  20 |  15.0 |
| 987654321 |  30 |  20.0 |
| 987987987 |  10 |  35.0 |
| 987987987 |  30 |   5.0 |
| 999887777 |  10 |  10.0 |
| 999887777 |  30 |  30.0 |
+-----------+-----+-------+
16 rows in set (0.00 sec)

Problems

  1. ์„ฑ์ด โ€˜Borgโ€™์ธ ์‚ฌ์›(๋“ค)์˜ ์ „์ฒด ์ด๋ฆ„(Fname, Minit, Lname)๊ณผ ์ƒ๋…„์›”์ผ(Bdate)๊ณผ ์ฃผ์†Œ(Address)๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋ผ.
    mysql> select Fname, Minit, Lname, Bdate, Address from employee WHERE Lname='Borg';
    +-------+-------+-------+------------+-----------------------+
    | Fname | Minit | Lname | Bdate      | Address               |
    +-------+-------+-------+------------+-----------------------+
    | James | E     | Borg  | 1937-11-10 | 450 Stone, Houston TX |
    +-------+-------+-------+------------+-----------------------+
    1 row in set (0.00 sec)
    
  2. ๋ฆด๋ ˆ์ด์…˜ EMPLOYEE์™€ DEPARTMENT์˜ ์นดํ‹ฐ์…˜ ๊ณฑ์„ ๊ฒ€์ƒ‰ํ•˜๋ผ.
    mysql> select * from Employee, Department;
    +----------+-------+---------+-----------+------------+-------------------------+------+--------+-----------+-----+----------------+---------+-----------+----------------+
    | Fname    | Minit | Lname   | Ssn       | Bdate      | Address                 | Sex  | Salary | Super_ssn | Dno | Dname          | Dnumber | Mgr_ssn   | Mgr_start_date |
    +----------+-------+---------+-----------+------------+-------------------------+------+--------+-----------+-----+----------------+---------+-----------+----------------+
    | John     | B     | Smith   | 123456789 | 1965-01-09 | 731 Fondren, Houston TX | M    |  30000 | 333445555 |   5 | Research       |       5 | 333445555 | 1988-05-22     |
    | John     | B     | Smith   | 123456789 | 1965-01-09 | 731 Fondren, Houston TX | M    |  30000 | 333445555 |   5 | Administration |       4 | 987654321 | 1995-01-01     |
    | John     | B     | Smith   | 123456789 | 1965-01-09 | 731 Fondren, Houston TX | M    |  30000 | 333445555 |   5 | Headquarters   |       1 | 888665555 | 1981-06-19     |
    | Franklin | T     | Wong    | 333445555 | 1965-12-08 | 638 Voss, Houston TX    | M    |  40000 | 888665555 |   5 | Research       |       5 | 333445555 | 1988-05-22     |
    | Franklin | T     | Wong    | 333445555 | 1965-12-08 | 638 Voss, Houston TX    | M    |  40000 | 888665555 |   5 | Administration |       4 | 987654321 | 1995-01-01     |
    | Franklin | T     | Wong    | 333445555 | 1965-12-08 | 638 Voss, Houston TX    | M    |  40000 | 888665555 |   5 | Headquarters   |       1 | 888665555 | 1981-06-19     |
    | Joyce    | A     | English | 453453453 | 1972-07-31 | 5631 Rice, Houston TX   | F    |  25000 | 333445555 |   5 | Research       |       5 | 333445555 | 1988-05-22     |
    | Joyce    | A     | English | 453453453 | 1972-07-31 | 5631 Rice, Houston TX   | F    |  25000 | 333445555 |   5 | Administration |       4 | 987654321 | 1995-01-01     |
    | Joyce    | A     | English | 453453453 | 1972-07-31 | 5631 Rice, Houston TX   | F    |  25000 | 333445555 |   5 | Headquarters   |       1 | 888665555 | 1981-06-19     |
    | Ramesh   | K     | Narayan | 666884444 | 1962-09-15 | 975 Fire Oak, Humble TX | M    |  38000 | 333445555 |   5 | Research       |       5 | 333445555 | 1988-05-22     |
    | Ramesh   | K     | Narayan | 666884444 | 1962-09-15 | 975 Fire Oak, Humble TX | M    |  38000 | 333445555 |   5 | Administration |       4 | 987654321 | 1995-01-01     |
    | Ramesh   | K     | Narayan | 666884444 | 1962-09-15 | 975 Fire Oak, Humble TX | M    |  38000 | 333445555 |   5 | Headquarters   |       1 | 888665555 | 1981-06-19     |
    | James    | E     | Borg    | 888665555 | 1937-11-10 | 450 Stone, Houston TX   | M    |  55000 | NULL      |   1 | Research       |       5 | 333445555 | 1988-05-22     |
    | James    | E     | Borg    | 888665555 | 1937-11-10 | 450 Stone, Houston TX   | M    |  55000 | NULL      |   1 | Administration |       4 | 987654321 | 1995-01-01     |
    | James    | E     | Borg    | 888665555 | 1937-11-10 | 450 Stone, Houston TX   | M    |  55000 | NULL      |   1 | Headquarters   |       1 | 888665555 | 1981-06-19     |
    | Jennifer | S     | Wallace | 987654321 | 1941-06-20 | 291 Berry, Bellaire TX  | F    |  43000 | 888665555 |   4 | Research       |       5 | 333445555 | 1988-05-22     |
    | Jennifer | S     | Wallace | 987654321 | 1941-06-20 | 291 Berry, Bellaire TX  | F    |  43000 | 888665555 |   4 | Administration |       4 | 987654321 | 1995-01-01     |
    | Jennifer | S     | Wallace | 987654321 | 1941-06-20 | 291 Berry, Bellaire TX  | F    |  43000 | 888665555 |   4 | Headquarters   |       1 | 888665555 | 1981-06-19     |
    | Ahmad    | V     | Jabbar  | 987987987 | 1969-03-29 | 980 Dallas, Houston TX  | M    |  25000 | 987654321 |   4 | Research       |       5 | 333445555 | 1988-05-22     |
    | Ahmad    | V     | Jabbar  | 987987987 | 1969-03-29 | 980 Dallas, Houston TX  | M    |  25000 | 987654321 |   4 | Administration |       4 | 987654321 | 1995-01-01     |
    | Ahmad    | V     | Jabbar  | 987987987 | 1969-03-29 | 980 Dallas, Houston TX  | M    |  25000 | 987654321 |   4 | Headquarters   |       1 | 888665555 | 1981-06-19     |
    | Alicia   | J     | Zelaya  | 999887777 | 1968-01-19 | 3321 Castle, Spring TX  | F    |  25000 | 987654321 |   4 | Research       |       5 | 333445555 | 1988-05-22     |
    | Alicia   | J     | Zelaya  | 999887777 | 1968-01-19 | 3321 Castle, Spring TX  | F    |  25000 | 987654321 |   4 | Administration |       4 | 987654321 | 1995-01-01     |
    | Alicia   | J     | Zelaya  | 999887777 | 1968-01-19 | 3321 Castle, Spring TX  | F    |  25000 | 987654321 |   4 | Headquarters   |       1 | 888665555 | 1981-06-19     |
    +----------+-------+---------+-----------+------------+-------------------------+------+--------+-----------+-----+----------------+---------+-----------+----------------+
    24 rows in set (0.00 sec)
    
  3. ๋ชจ๋“  ์‚ฌ์›๋“ค์˜ ์‚ฌ๋ฒˆ(SSN)๊ณผ ๊ทธ๋“ค์ด ์†ํ•œ ์†Œ์† ๋ถ€์„œ(DNAME)๋“ค์„ ๋ชจ๋‘ ๊ฒ€์ƒ‰ํ•˜๋ผ.
    mysql> select Ssn, Dname from employee, department WHERE Dno=Dnumber;
    +-----------+----------------+
    | Ssn       | Dname          |
    +-----------+----------------+
    | 987654321 | Administration |
    | 987987987 | Administration |
    | 999887777 | Administration |
    | 888665555 | Headquarters   |
    | 123456789 | Research       |
    | 333445555 | Research       |
    | 453453453 | Research       |
    | 666884444 | Research       |
    +-----------+----------------+
    8 rows in set (0.00 sec)
    
  4. Headquarters ๋ถ€์„œ์—์„œ ๊ทผ๋ฌดํ•˜๋Š” ๋ชจ๋“  ์‚ฌ์›์˜ ์ด๋ฆ„(Fname, Lname)๊ณผ ์ฃผ์†Œ(Address)๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋ผ.
    mysql> select Fname, Lname, Address from employee, department WHERE Dno=Dnumber AND Dname='Headquarters';
    +-------+-------+-----------------------+
    | Fname | Lname | Address               |
    +-------+-------+-----------------------+
    | James | Borg  | 450 Stone, Houston TX |
    +-------+-------+-----------------------+
    1 row in set (0.00 sec)
    
  5. Administration ๋ถ€์„œ์—์„œ ์ผํ•˜๋Š” ์‚ฌ์›๋“ค์˜ ์„ฑ(Lname)๊ณผ ์ด๋ฆ„(Fname), ๊ทธ๋ฆฌ๊ณ  ๊ด€๋ฆฌ์ž๋ฅผ ์‹œ์ž‘ํ•œ ๋‚ (mgr_start_date)์„ ๊ฒ€์ƒ‰ํ•˜๋ผ.
    mysql> select Lname, Fname, Mgr_start_date from employee, department WHERE Dno=Dnumber AND Dname='Administration';
    +---------+----------+----------------+
    | Lname   | Fname    | Mgr_start_date |
    +---------+----------+----------------+
    | Wallace | Jennifer | 1995-01-01     |
    | Jabbar  | Ahmad    | 1995-01-01     |
    | Zelaya  | Alicia   | 1995-01-01     |
    +---------+----------+----------------+
    3 rows in set (0.00 sec)
    
  6. ๊ฐ ์‚ฌ์›์— ๋Œ€ํ•ด ์‚ฌ์›์˜ ์ด๋ฆ„(Fname)๊ณผ ์„ฑ(Lname), ์ง์† ์ƒ์‚ฌ์˜ ์ด๋ฆ„ (Fname)๊ณผ ์„ฑ(Lname)์„ ๊ฒ€์ƒ‰ํ•˜๋ผ.

    mysql> select E.Fname, E.Lname, S.Fname, S.Lname from Employee AS E, Employee AS S WHERE E.Super_ssn = S.Ssn;
    +----------+---------+----------+---------+
    | Fname    | Lname   | Fname    | Lname   |
    +----------+---------+----------+---------+
    | John     | Smith   | Franklin | Wong    |
    | Franklin | Wong    | James    | Borg    |
    | Joyce    | English | Franklin | Wong    |
    | Ramesh   | Narayan | Franklin | Wong    |
    | Jennifer | Wallace | James    | Borg    |
    | Ahmad    | Jabbar  | Jennifer | Wallace |
    | Alicia   | Zelaya  | Jennifer | Wallace |
    +----------+---------+----------+---------+
    7 rows in set (0.01 sec)
    
    mysql> select E.Fname AS Employee_Fname, E.Lname AS Employee_Lname, S.Fname AS Super_Fname, S.Lname AS Super_Lname from Employee AS E, Employee AS S WHERE E.Super_ssn = S.Ssn;
    +----------------+----------------+-------------+-------------+
    | Employee_Fname | Employee_Lname | Super_Fname | Super_Lname |
    +----------------+----------------+-------------+-------------+
    | John           | Smith          | Franklin    | Wong        |
    | Franklin       | Wong           | James       | Borg        |
    | Joyce          | English        | Franklin    | Wong        |
    | Ramesh         | Narayan        | Franklin    | Wong        |
    | Jennifer       | Wallace        | James       | Borg        |
    | Ahmad          | Jabbar         | Jennifer    | Wallace     |
    | Alicia         | Zelaya         | Jennifer    | Wallace     |
    +----------------+----------------+-------------+-------------+
    7 rows in set (0.00 sec)
    
  7. โ€˜Houstonโ€™์— ์œ„์น˜ํ•œ ๋ชจ๋“  ํ”„๋กœ์ ํŠธ์— ๋Œ€ํ•ด์„œ ํ”„๋กœ์ ํŠธ ๋ฒˆํ˜ธ(Pnumber), ๋‹ด๋‹น ๋ถ€์„œ ๋ฒˆํ˜ธ(Dnum), ๋ถ€์„œ ๊ด€๋ฆฌ์ž์˜ ์„ฑ(Lname), ์ฃผ์†Œ(Address), ์ƒ๋…„์›”์ผ (Bdate)์„ ๊ฒ€์ƒ‰ํ•˜๋ผ.
    mysql> select P.Pnumber, P.Dnum, E.Lname, E.Address, E.Bdate from project as P, employee as E, department as D WHERE P.Plocation='Houston' AND P.Dnum=D.Dnumber AND D.Mgr_ssn = E.Ssn;
    +---------+------+-------+-----------------------+------------+
    | Pnumber | Dnum | Lname | Address               | Bdate      |
    +---------+------+-------+-----------------------+------------+
    |       3 |    5 | Wong  | 638 Voss, Houston TX  | 1965-12-08 |
    |      20 |    1 | Borg  | 450 Stone, Houston TX | 1937-11-10 |
    +---------+------+-------+-----------------------+------------+
    2 rows in set (0.00 sec)
    
  8. ์ผ๋ฐ˜ ์‚ฌ์›์ด๋“  ๊ด€๋ฆฌ์ž์ด๋“  ๊ฐ„์— ์„ฑ(Lname)์ด โ€˜Smithโ€™์ธ ์‚ฌ์›์ด ์ˆ˜ํ–‰ํ•˜๋Š” ํ”„๋กœ์ ํŠธ ๋ฒˆํ˜ธ(Pnumber)์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋ผ. (hint : UNION)
    mysql> (select Pnumber from project, works_on, employee where Pnumber=Pno and Essn=Ssn and Lname='Smith')
       -> UNION
       -> (select Pnumber from project, department, employee where Dnum=Dnumber and Mgr_ssn=Ssn and Lname='Smith');
    +---------+
    | Pnumber |
    +---------+
    |       1 |
    |       2 |
    +---------+
    2 rows in set (0.00 sec)
    
  9. ์ฃผ์†Œ์— โ€˜Houston TXโ€™๊ฐ€ ํฌํ•จ๋˜๋Š” ๋ชจ๋“  ์‚ฌ์›(Fname, Lname)์„ ๊ฒ€์ƒ‰ํ•˜๋ผ.

    mysql> select Fname, Lname from Employee where Address LIKE '%Houston TX%';
    +----------+---------+
    | Fname    | Lname   |
    +----------+---------+
    | John     | Smith   |
    | Franklin | Wong    |
    | Joyce    | English |
    | James    | Borg    |
    | Ahmad    | Jabbar  |
    +----------+---------+
    5 rows in set (0.00 sec)
    
    mysql> -- ํ™•์ธ ๋ฐ ๊ฒ€์ฆํ•˜๊ธฐ --
    mysql> select Fname, Lname, Address from Employee where Address LIKE '%Houston TX%';
    +----------+---------+-------------------------+
    | Fname    | Lname   | Address                 |
    +----------+---------+-------------------------+
    | John     | Smith   | 731 Fondren, Houston TX |
    | Franklin | Wong    | 638 Voss, Houston TX    |
    | Joyce    | English | 5631 Rice, Houston TX   |
    | James    | Borg    | 450 Stone, Houston TX   |
    | Ahmad    | Jabbar  | 980 Dallas, Houston TX  |
    +----------+---------+-------------------------+
    5 rows in set (0.00 sec)
    
  10. 1960๋…„๋Œ€ ํƒœ์–ด๋‚œ ๋ชจ๋“  ์‚ฌ์›์˜ ์ „์ฒด ์ด๋ฆ„(Fname, Lname)์„ ๊ฒ€์ƒ‰ํ•˜๋ผ.

    mysql> select Fname, Lname from employee where Bdate LIKE '196%';
    +----------+---------+
    | Fname    | Lname   |
    +----------+---------+
    | John     | Smith   |
    | Franklin | Wong    |
    | Ramesh   | Narayan |
    | Ahmad    | Jabbar  |
    | Alicia   | Zelaya  |
    +----------+---------+
    5 rows in set (0.00 sec)
    
    mysql> -- ํ™•์ธ ๋ฐ ๊ฒ€์ฆํ•˜๊ธฐ --
    mysql> select Fname, Lname, Bdate from employee where Bdate LIKE '196%';
    +----------+---------+------------+
    | Fname    | Lname   | Bdate      |
    +----------+---------+------------+
    | John     | Smith   | 1965-01-09 |
    | Franklin | Wong    | 1965-12-08 |
    | Ramesh   | Narayan | 1962-09-15 |
    | Ahmad    | Jabbar  | 1969-03-29 |
    | Alicia   | Zelaya  | 1968-01-19 |
    +----------+---------+------------+
    5 rows in set (0.01 sec)
    

Source

  1. Fundamentals of Database Systems 7th Edition by Ramez Elmasri, Shamkant B. Navathe.
  2. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‹œ์Šคํ…œ (Database System Concepts) 6th Edition by Abranham Silberschatz, Henry F.Korth, S.Sudarchan, ๊น€ํ˜•์ฃผ ์—ญ.
  3. [gyoogle GitHub tech-interview-for-developer/Computer Science/Database/[Database SQL] JOIN.md](https://github.com/gyoogle/tech-interview-for-developer/blob/master/Computer%20Science/Database/%5BDatabase%20SQL%5D%20JOIN.md)
  4. [carrot.log [Database] JOIN์˜ ์ข…๋ฅ˜ : INNER, OUTER, CROSS, SELF](https://velog.io/@yanghl98/Database-JOIN)
  5. [FRANKโ€™S BLOG ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์กฐ์ธ(join)์ด๋ž€?](https://blog.advenoh.pe.kr/database/%EA%B4%80%EA%B3%84%ED%98%95-%EB%8D%B0%EC%9D%B4%ED%84%B0%EB%B2%A0%EC%9D%B4%EC%8A%A4%EC%97%90%EC%84%9C-%EC%A1%B0%EC%9D%B8-join%EC%9D%B4%EB%9E%80/)
  6. [C.L. Moffatt Visual Representation of SQL Joins](https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins)
  7. https://swingswing.tistory.com/12
  8. [๊ฑฐ๊พธ๋กœ ๋ฐ”๋ผ๋ณธ ์„ธ์ƒ [SQL] Join(์กฐ์ธ)](https://clairdelunes.tistory.com/22)