Computer-Science

  1. Complex SQL
  2. Practice

Complex SQL


NULL 값을 ν¬ν•¨ν•œ 비ꡐ

쀑첩 질의(nested query)와 집합 비ꡐ

쀑첩 질의

비ꡐ μ—°μ‚°μž IN

= ALL μ—°μ‚°μž

= ANY(= SOME) μ—°μ‚°μž

상관 쀑첩 질의 (Correlated Nested Query)

SQL의 EXISTS ν•¨μˆ˜μ™€ UNIQUE ν•¨μˆ˜

EXISTS ν•¨μˆ˜

SQLμ—μ„œ λͺ…μ‹œμ  집합과 μ• νŠΈλ¦¬λ·°νŠΈμ˜ 재λͺ…λͺ…

집계 ν•¨μˆ˜(Aggregate functions)

Grouping: Group by와 Having 절


Complex 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. β€˜Newbenefits’ ν”„λ‘œμ νŠΈμ— μ°Έμ—¬ν•˜λŠ” λͺ¨λ“  μ‚¬μ›μ˜ κΈ‰μ—¬λ₯Ό 10% 올린 경우의 κΈ‰μ—¬λ₯Ό μ œμ‹œν•˜λΌ. (Fname, Lname, Increased_sal)
    mysql> select Fname, Lname, Salary*1.1 as Increased_sal from project, works_on, employee where Pname='Newbenefits' and Pnumber=Pno and Essn=Ssn;
    +----------+---------+---------------+
    | Fname    | Lname   | Increased_sal |
    +----------+---------+---------------+
    | Jennifer | Wallace |       47300.0 |
    | Ahmad    | Jabbar  |       27500.0 |
    | Alicia   | Zelaya  |       27500.0 |
    +----------+---------+---------------+
    3 rows in set (0.00 sec)
    
  2. κΈ‰μ—¬κ°€ 30,000λ‹¬λŸ¬μ—μ„œ 40,000λ‹¬λŸ¬ 사이에 μžˆλŠ” 5번 λΆ€μ„œμ˜ λͺ¨λ“  사원을 κ²€μƒ‰ν•˜λΌ.
    mysql> select * from employee WHERE (Salary BETWEEN 30000 AND 40000) AND Dno=5;
    +----------+-------+---------+-----------+------------+-------------------------+------+--------+-----------+-----+
    | 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 |
    | Ramesh   | K     | Narayan | 666884444 | 1962-09-15 | 975 Fire Oak, Humble TX | M    |  38000 | 333445555 |   5 |
    +----------+-------+---------+-----------+------------+-------------------------+------+--------+-----------+-----+
    3 rows in set (0.00 sec)
    
    mysql> select * from employee WHERE Salary>=30000 AND Salary<=40000 AND Dno=5;
    +----------+-------+---------+-----------+------------+-------------------------+------+--------+-----------+-----+
    | 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 |
    | Ramesh   | K     | Narayan | 666884444 | 1962-09-15 | 975 Fire Oak, Humble TX | M    |  38000 | 333445555 |   5 |
    +----------+-------+---------+-----------+------------+-------------------------+------+--------+-----------+-----+
    3 rows in set (0.00 sec)
    
  3. λͺ¨λ“  μ‚¬μ›μ˜ (1) κΈ‰μ—¬(높은 μˆœμ„œ) (2) 생년월일(λ‚˜μ΄κ°€ λ§Žμ€ μˆœμ„œ)을 μ œμ‹œν•˜λΌ.
    mysql> select Salary, Bdate from employee ORDER BY Salary DESC, Bdate ASC;
    +--------+------------+
    | Salary | Bdate      |
    +--------+------------+
    |  55000 | 1937-11-10 |
    |  43000 | 1941-06-20 |
    |  40000 | 1965-12-08 |
    |  38000 | 1962-09-15 |
    |  30000 | 1965-01-09 |
    |  25000 | 1968-01-19 |
    |  25000 | 1969-03-29 |
    |  25000 | 1972-07-31 |
    +--------+------------+
    8 rows in set (0.00 sec)
    
  4. 상사가 μ—†λŠ” λͺ¨λ“  μ‚¬μ›μ˜ 이름(Fname, Lname)을 κ²€μƒ‰ν•˜λΌ.
    mysql> select Fname, Lname from employee where Super_ssn IS NULL;
    +-------+-------+
    | Fname | Lname |
    +-------+-------+
    | James | Borg  |
    +-------+-------+
    1 row in set (0.00 sec)
    
  5. λΆ€μ–‘κ°€μ‘±μ˜ 성별(Sex)κ³Ό μ‚¬μ›μ˜ 성별이 같은 μ‚¬μ›μ˜ 이름(Fname, Lname)을 κ²€μƒ‰ν•˜λΌ.
    mysql> select D.Sex, E.Fname, E.Lname from dependent as D, employee as E where D.Essn=E.Ssn and D.Sex=E.Sex;
    +------+----------+-------+
    | Sex  | Fname    | Lname |
    +------+----------+-------+
    | M    | John     | Smith |
    | M    | Franklin | Wong  |
    +------+----------+-------+
    2 rows in set (0.00 sec)
    
    mysql> select D.Sex, E.Fname, E.Lname from dependent AS D, employee as E where E.Ssn IN (select Essn where D.Sex=E.Sex);
    +------+----------+-------+
    | Sex  | Fname    | Lname |
    +------+----------+-------+
    | M    | John     | Smith |
    | M    | Franklin | Wong  |
    +------+----------+-------+
    2 rows in set (0.00 sec)
    
    mysql> select D.Sex, E.Fname, E.Lname from dependent AS D, employee as E where EXISTS (select * where D.Essn=E.Ssn and D.Sex=E.Sex);
    +------+----------+-------+
    | Sex  | Fname    | Lname |
    +------+----------+-------+
    | M    | John     | Smith |
    | M    | Franklin | Wong  |
    +------+----------+-------+
    2 rows in set (0.00 sec)
    
  6. 뢀양가쑱이 μ—†λŠ” μ’…μ—…μ›λ“€μ˜ 이름(Fname, Lname)을 κ²€μƒ‰ν•˜λΌ.
    mysql> select Fname, Lname from employee where NOT EXISTS (select * from dependent where Ssn=Essn);
    +--------+---------+
    | Fname  | Lname   |
    +--------+---------+
    | Joyce  | English |
    | Ramesh | Narayan |
    | James  | Borg    |
    | Ahmad  | Jabbar  |
    | Alicia | Zelaya  |
    +--------+---------+
    5 rows in set (0.00 sec)
    
  7. ν”„λ‘œμ νŠΈ 번호 1,2,3μ—μ„œ μΌν•˜λŠ” μ‚¬μ›μ˜ μ£Όλ―Όλ“±λ‘λ²ˆν˜Έ(Essn)λ₯Ό κ²€μƒ‰ν•˜λΌ.
    mysql> select Distinct(Essn) from works_on WHERE Pno<=3;
    +-----------+
    | Essn      |
    +-----------+
    | 123456789 |
    | 453453453 |
    | 333445555 |
    | 666884444 |
    +-----------+
    4 rows in set (0.01 sec)
    
    mysql> select Distinct(Essn) from works_on WHERE Pno IN (1,2,3);
    +-----------+
    | Essn      |
    +-----------+
    | 123456789 |
    | 453453453 |
    | 333445555 |
    | 666884444 |
    +-----------+
    4 rows in set (0.00 sec)
    
  8. μ‚¬μ›μ˜ κΈ‰μ—¬μ˜ ν•©, 졜고 κΈ‰μ—¬, μ΅œμ € κΈ‰μ—¬, 평균 κΈ‰μ—¬λ₯Ό κ΅¬ν•˜λΌ.
    mysql> select SUM(Salary), MAX(Salary), MIN(Salary), AVG(Salary) from employee;
    +-------------+-------------+-------------+-------------+
    | SUM(Salary) | MAX(Salary) | MIN(Salary) | AVG(Salary) |
    +-------------+-------------+-------------+-------------+
    |      281000 |       55000 |       25000 |  35125.0000 |
    +-------------+-------------+-------------+-------------+
    1 row in set (0.00 sec)
    
  9. νšŒμ‚¬μ˜ 총 μ‚¬μ›μˆ˜λ₯Ό μ œμ‹œν•˜λΌ.
    mysql> select COUNT(*) from employee;
    +----------+
    | COUNT(*) |
    +----------+
    |        8 |
    +----------+
    1 row in set (0.02 sec)
    
  10. 각 λΆ€μ„œμ—μ„œ κ·Όλ¬΄ν•˜λŠ” μ‚¬μ›μ˜ 수λ₯Ό κ²€μƒ‰ν•˜λΌ. (λΆ€μ„œμ΄λ¦„κ³Ό μ†Œμ† μ‚¬μ›μˆ˜λ₯Ό μ œμ‹œ)
    mysql> select Dname, COUNT(*) from department, employee WHERE Dno=Dnumber GROUP BY Dname;
    +----------------+----------+
    | Dname          | COUNT(*) |
    +----------------+----------+
    | Administration |        3 |
    | Headquarters   |        1 |
    | Research       |        4 |
    +----------------+----------+
    3 rows in set (0.00 sec)
    
  11. 각 λΆ€μ„œμ— λŒ€ν•΄μ„œ λΆ€μ„œμ΄λ¦„, λΆ€μ„œμ— μ†Œμ†λœ μ‚¬μ›μ˜ μˆ˜μ™€ 졜고 급여와 평균 κΈ‰μ—¬λ₯Ό κ΅¬ν•˜λΌ.
    mysql> select Dname, COUNT(*), MAX(Salary), AVG(Salary) from department, employee WHERE Dno=Dnumber GROUP BY Dname;
    +----------------+----------+-------------+-------------+
    | Dname          | COUNT(*) | MAX(Salary) | AVG(Salary) |
    +----------------+----------+-------------+-------------+
    | Administration |        3 |       43000 |  31000.0000 |
    | Headquarters   |        1 |       55000 |  55000.0000 |
    | Research       |        4 |       40000 |  33250.0000 |
    +----------------+----------+-------------+-------------+
    3 rows in set (0.01 sec)
    
  12. ν”„λ‘œμ νŠΈμ— λŒ€ν•΄μ„œ ν”„λ‘œμ νŠΈ 번호, ν”„λ‘œμ νŠΈ 이름, κ·Έ ν”„λ‘œμ νŠΈμ—μ„œ 근무 ν•˜λŠ” μ‚¬μ›λ“€μ˜ 수λ₯Ό κ²€μƒ‰ν•˜λΌ.
    mysql> select Pnumber, Pname, COUNT(*) from project, employee WHERE Dnum=Dno GROUP BY Pnumber, Pname;
    +---------+-----------------+----------+
    | Pnumber | Pname           | COUNT(*) |
    +---------+-----------------+----------+
    |       1 | ProductX        |        4 |
    |       2 | ProductY        |        4 |
    |       3 | ProductZ        |        4 |
    |      10 | Computerization |        3 |
    |      20 | Reorganization  |        1 |
    |      30 | Newbenefits     |        3 |
    +---------+-----------------+----------+
    6 rows in set (0.01 sec)
    
  13. μ„Έ λͺ… μ΄μƒμ˜ 사원이 κ·Όλ¬΄ν•˜λŠ” ν”„λ‘œμ νŠΈμ— λŒ€ν•΄μ„œ ν”„λ‘œμ νŠΈ 번호, ν”„λ‘œμ νŠΈ 이름, κ·Έ ν”„λ‘œμ νŠΈμ—μ„œ κ·Όλ¬΄ν•˜λŠ” μ‚¬μ›λ“€μ˜ 수λ₯Ό κ²€μƒ‰ν•˜λΌ.
    mysql> select Pnumber, Pname, COUNT(*) from project, employee WHERE Dnum=Dno AND Dno IN
       -> (select Dno from employee GROUP BY Dno HAVING COUNT(*)>=3)
       -> GROUP BY Pnumber, Pname;
    +---------+-----------------+----------+
    | Pnumber | Pname           | COUNT(*) |
    +---------+-----------------+----------+
    |       1 | ProductX        |        4 |
    |       2 | ProductY        |        4 |
    |       3 | ProductZ        |        4 |
    |      10 | Computerization |        3 |
    |      30 | Newbenefits     |        3 |
    +---------+-----------------+----------+
    5 rows in set (0.03 sec)
    
  14. ν”„λ‘œμ νŠΈμ— λŒ€ν•΄μ„œ ν”„λ‘œμ νŠΈ 번호, ν”„λ‘œμ νŠΈ 이름, 5번 λΆ€μ„œμ— μ†ν•˜λ©΄μ„œ ν”„λ‘œμ νŠΈμ—μ„œ κ·Όλ¬΄ν•˜λŠ” μ‚¬μ›μ˜ 수λ₯Ό κ²€μƒ‰ν•˜λΌ.
    mysql> select Pnumber, Pname, COUNT(*) from project, employee, works_on WHERE Pnumber=Pno and Ssn=Essn and Dno=5 GROUP BY Pnumber, Pname;
    +---------+-----------------+----------+
    | Pnumber | Pname           | COUNT(*) |
    +---------+-----------------+----------+
    |       1 | ProductX        |        2 |
    |       2 | ProductY        |        3 |
    |       3 | ProductZ        |        2 |
    |      10 | Computerization |        1 |
    |      20 | Reorganization  |        1 |
    +---------+-----------------+----------+
    5 rows in set (0.01 sec)
    
  15. 3λͺ… μ΄μƒμ˜ 사원이 κ·Όλ¬΄ν•˜λŠ” 각 λΆ€μ„œμ— λŒ€ν•΄μ„œ λΆ€μ„œ λ²ˆν˜Έμ™€ 40,000λ‹¬λŸ¬κ°€ λ„˜λŠ” κΈ‰μ—¬λ₯Ό λ°›λŠ” μ‚¬μ›μ˜ ssn 및 salaryλ₯Ό κ²€μƒ‰ν•˜λΌ.
    mysql> select Dno, Ssn, Salary from employee WHERE Salary>40000 AND Dno IN
      -> (select Dno from employee GROUP BY Dno HAVING COUNT(*)>=3)
      -> GROUP BY Dno, Ssn, Salary;
    +-----+-----------+--------+
    | Dno | Ssn       | Salary |
    +-----+-----------+--------+
    |   4 | 987654321 |  43000 |
    +-----+-----------+--------+
    1 row in set (0.00 sec)
    

SELECTλ¬Έ μš”μ•½


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, κΉ€ν˜•μ£Ό μ—­.