select a.sname from student1 a, professor b where a.pid=b.pid;
MariaDB [example]> select a.sname from student1 a, professor b where a.pid=b.pid;
+--------+
| sname |
+--------+
| stella |
| eve |
| John |
| stella |
| eve |
| John |
+--------+
6 rows in set (0.000 sec)
MariaDB [example]> select * from student1 a right join professor b on (a.pid=b.pid);
+------+--------+-------------+------+--------+
| pid | sname | mydept | pid | pname |
+------+--------+-------------+------+--------+
| 1 | stella | management | 1 | tome |
| 1 | stella | management | 1 | Prof A |
| 2 | eve | engineering | 2 | rosa |
| 2 | eve | engineering | 2 | Prof B |
| 3 | John | IT | 3 | sun |
| 3 | John | IT | 3 | Prof C |
+------+--------+-------------+------+--------+
6 rows in set (0.004 sec)
MariaDB [example]> select * from student1 a right join professor b on (a.pid=b.pid);
+------+--------+-------------+------+--------+
| pid | sname | mydept | pid | pname |
+------+--------+-------------+------+--------+
| 1 | stella | management | 1 | tome |
| 1 | stella | management | 1 | Prof A |
| 2 | eve | engineering | 2 | rosa |
| 2 | eve | engineering | 2 | Prof B |
| 3 | John | IT | 3 | sun |
| 3 | John | IT | 3 | Prof C |
+------+--------+-------------+------+--------+
6 rows in set (0.000 sec)
MariaDB [example]> select * from professor a left join student1 b on (a.pid=b.pid);
+------+--------+------+--------+-------------+
| pid | pname | pid | sname | mydept |
+------+--------+------+--------+-------------+
| 1 | tome | 1 | stella | management |
| 1 | Prof A | 1 | stella | management |
| 2 | rosa | 2 | eve | engineering |
| 2 | Prof B | 2 | eve | engineering |
| 3 | sun | 3 | John | IT |
| 3 | Prof C | 3 | John | IT |
+------+--------+------+--------+-------------+
6 rows in set (0.002 sec)
수업 학생 클래스
-------------------------
Enter password: ******
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.6.3-MariaDB mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use test
Database changed
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| dept |
| professor |
| student |
| t_student |
+----------------+
4 rows in set (0.010 sec)
MariaDB [test]> select * from student;
+------+-------+------+
| sid | sname | pid |
+------+-------+------+
| 1 | Kim | 1 |
| 2 | Park | 2 |
| 3 | Lee | 1 |
| 4 | Kang | NULL |
+------+-------+------+
4 rows in set (0.012 sec)
MariaDB [test]> select * from student;
+------+-------+------+
| sid | sname | pid |
+------+-------+------+
| 1 | Kim | 1 |
| 2 | Park | 2 |
| 3 | Lee | 1 |
| 4 | Kang | NULL |
+------+-------+------+
4 rows in set (0.000 sec)
MariaDB [test]> select * from student join professor on (student.pid=professor.pid);
+------+-------+------+------+--------+
| sid | sname | pid | pid | pname |
+------+-------+------+------+--------+
| 1 | Kim | 1 | 1 | Prof A |
| 2 | Park | 2 | 2 | Prof B |
| 3 | Lee | 1 | 1 | Prof A |
+------+-------+------+------+--------+
3 rows in set (0.011 sec)
MariaDB [test]> select * from student join professor;
+------+-------+------+------+--------+
| sid | sname | pid | pid | pname |
+------+-------+------+------+--------+
| 1 | Kim | 1 | 1 | Prof A |
| 1 | Kim | 1 | 2 | Prof B |
| 2 | Park | 2 | 1 | Prof A |
| 2 | Park | 2 | 2 | Prof B |
| 3 | Lee | 1 | 1 | Prof A |
| 3 | Lee | 1 | 2 | Prof B |
| 4 | Kang | NULL | 1 | Prof A |
| 4 | Kang | NULL | 2 | Prof B |
+------+-------+------+------+--------+
8 rows in set (0.000 sec)
MariaDB [test]> select * from student join professor on (student.pid=professor.pid);
+------+-------+------+------+--------+
| sid | sname | pid | pid | pname |
+------+-------+------+------+--------+
| 1 | Kim | 1 | 1 | Prof A |
| 2 | Park | 2 | 2 | Prof B |
| 3 | Lee | 1 | 1 | Prof A |
+------+-------+------+------+--------+
3 rows in set (0.000 sec)
MariaDB [test]> select a.sname, b.pname from student a join professor b on (student.pid=professor.pid) where b.pname ='Prof A';
ERROR 1054 (42S22): Unknown column 'student.pid' in 'on clause'
MariaDB [test]> select a.sname, b.pname from student a join professor b on (a.pid=b.pid) where b.pname ='Prof A';
+-------+--------+
| sname | pname |
+-------+--------+
| Kim | Prof A |
| Lee | Prof A |
+-------+--------+
2 rows in set (0.001 sec)
MariaDB [test]> select * from student join professor on (student.pid=professor.pid);
+------+-------+------+------+--------+
| sid | sname | pid | pid | pname |
+------+-------+------+------+--------+
| 1 | Kim | 1 | 1 | Prof A |
| 2 | Park | 2 | 2 | Prof B |
| 3 | Lee | 1 | 1 | Prof A |
+------+-------+------+------+--------+
3 rows in set (0.000 sec)
MariaDB [test]> create view advisor as select * from student join professor on (student.pid=professor.pid);
ERROR 1060 (42S21): Duplicate column name 'pid'
MariaDB [test]> create view advisor as select * from student a join professor b on (a.pid=b.pid);
ERROR 1060 (42S21): Duplicate column name 'pid'
MariaDB [test]> create view advisor as select a.sname, b.pname from student a join professor b on (a.pid=b.pid);
Query OK, 0 rows affected (0.013 sec)
MariaDB [test]> desc advisor;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sname | varchar(16) | YES | | NULL | |
| pname | varchar(16) | YES | | NULL |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.010 sec)
MariaDB [test]> select * from advisor;
+-------+--------+
| sname | pname |
+-------+--------+
| Kim | Prof A |
| Park | Prof B |
| Lee | Prof A |
+-------+--------+
3 rows in set (0.000 sec)
MariaDB [test]>
MariaDB [test]>
MariaDB [test]>
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| advisor |
| dept |
| professor |
| student |
| t_student |
+----------------+
5 rows in set (0.001 sec)
MariaDB [test]> show full tables;
+----------------+------------+
| Tables_in_test | Table_type |
+----------------+------------+
| advisor | VIEW |
| dept | BASE TABLE |
| professor | BASE TABLE |
| student | BASE TABLE |
| t_student | BASE TABLE |
+----------------+------------+
5 rows in set (0.002 sec)
MariaDB [test]> select * from advisor;
+-------+--------+
| sname | pname |
+-------+--------+
| Kim | Prof A |
| Park | Prof B |
| Lee | Prof A |
+-------+--------+
3 rows in set (0.000 sec)
MariaDB [test]> select * from student;
+------+-------+------+
| sid | sname | pid |
+------+-------+------+
| 1 | Kim | 1 |
| 2 | Park | 2 |
| 3 | Lee | 1 |
| 4 | Kang | NULL |
+------+-------+------+
4 rows in set (0.000 sec)
MariaDB [test]> create table lecture
-> (lid int, ltitle varchar(16));
Query OK, 0 rows affected (0.027 sec)
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| advisor |
| dept |
| lecture |
| professor |
| student |
| t_student |
+----------------+
6 rows in set (0.000 sec)
MariaDB [test]> create table class
-> (sid int, lid int, grade float);
Query OK, 0 rows affected (0.014 sec)
MariaDB [test]> desc class;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| sid | int(11) | YES | | NULL | |
| lid | int(11) | YES | | NULL | |
| grade | float | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.004 sec)
MariaDB [test]> insert into lecture values
-> (1, "DB"), (2, "ART");
Query OK, 2 rows affected (0.004 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [test]> select * from lecture;
+------+--------+
| lid | ltitle |
+------+--------+
| 1 | DB |
| 2 | ART |
+------+--------+
2 rows in set (0.000 sec)
MariaDB [test]> select * from student;
+------+-------+------+
| sid | sname | pid |
+------+-------+------+
| 1 | Kim | 1 |
| 2 | Park | 2 |
| 3 | Lee | 1 |
| 4 | Kang | NULL |
+------+-------+------+
4 rows in set (0.000 sec)
MariaDB [test]> insert into class values
-> (1, 1, 3.1), (1, 2, 4.5), (2, 1, 4.3), (3, 2, 4.1);
Query OK, 4 rows affected (0.003 sec)
Records: 4 Duplicates: 0 Warnings: 0
MariaDB [test]> select * from class;
+------+------+-------+
| sid | lid | grade |
+------+------+-------+
| 1 | 1 | 3.1 |
| 1 | 2 | 4.5 |
| 2 | 1 | 4.3 |
| 3 | 2 | 4.1 |
+------+------+-------+
4 rows in set (0.000 sec)
MariaDB [test]> show full tables;
+----------------+------------+
| Tables_in_test | Table_type |
+----------------+------------+
| advisor | VIEW |
| class | BASE TABLE |
| dept | BASE TABLE |
| lecture | BASE TABLE |
| professor | BASE TABLE |
| student | BASE TABLE |
| t_student | BASE TABLE |
+----------------+------------+
7 rows in set (0.001 sec)
MariaDB [test]> drop table t_student;
Query OK, 0 rows affected (0.015 sec)
MariaDB [test]> drop view advisor;
Query OK, 0 rows affected (0.007 sec)
MariaDB [test]> show full tables;
+----------------+------------+
| Tables_in_test | Table_type |
+----------------+------------+
| class | BASE TABLE |
| dept | BASE TABLE |
| lecture | BASE TABLE |
| professor | BASE TABLE |
| student | BASE TABLE |
+----------------+------------+
5 rows in set (0.001 sec)
MariaDB [test]> select * from student;
+------+-------+------+
| sid | sname | pid |
+------+-------+------+
| 1 | Kim | 1 |
| 2 | Park | 2 |
| 3 | Lee | 1 |
| 4 | Kang | NULL |
+------+-------+------+
4 rows in set (0.000 sec)
MariaDB [test]> select * from professor;
+------+--------+
| pid | pname |
+------+--------+
| 1 | Prof A |
| 2 | Prof B |
+------+--------+
2 rows in set (0.000 sec)
MariaDB [test]> insert into professor values (3, "Prof C");
Query OK, 1 row affected (0.001 sec)
MariaDB [test]> select * from professor;
+------+--------+
| pid | pname |
+------+--------+
| 1 | Prof A |
| 2 | Prof B |
| 3 | Prof C |
+------+--------+
3 rows in set (0.000 sec)
MariaDB [test]> select * from student a JOIN professor b ON (a.pid=b.pid);
+------+-------+------+------+--------+
| sid | sname | pid | pid | pname |
+------+-------+------+------+--------+
| 1 | Kim | 1 | 1 | Prof A |
| 2 | Park | 2 | 2 | Prof B |
| 3 | Lee | 1 | 1 | Prof A |
+------+-------+------+------+--------+
3 rows in set (0.000 sec)
MariaDB [test]> select * from student a RIGHT JOIN professor b ON (a.pid=b.pid);
+------+-------+------+------+--------+
| sid | sname | pid | pid | pname |
+------+-------+------+------+--------+
| 1 | Kim | 1 | 1 | Prof A |
| 2 | Park | 2 | 2 | Prof B |
| 3 | Lee | 1 | 1 | Prof A |
| NULL | NULL | NULL | 3 | Prof C |
+------+-------+------+------+--------+
4 rows in set (0.001 sec)
MariaDB [test]> select * from professor a left JOIN student b ON (a.pid=b.pid);
+------+--------+------+-------+------+
| pid | pname | sid | sname | pid |
+------+--------+------+-------+------+
| 1 | Prof A | 1 | Kim | 1 |
| 2 | Prof B | 2 | Park | 2 |
| 1 | Prof A | 3 | Lee | 1 |
| 3 | Prof C | NULL | NULL | NULL |
+------+--------+------+-------+------+
4 rows in set (0.001 sec)
MariaDB [test]> select * from professor a right JOIN student b ON (a.pid=b.pid);
+------+--------+------+-------+------+
| pid | pname | sid | sname | pid |
+------+--------+------+-------+------+
| 1 | Prof A | 1 | Kim | 1 |
| 1 | Prof A | 3 | Lee | 1 |
| 2 | Prof B | 2 | Park | 2 |
| NULL | NULL | 4 | Kang | NULL |
+------+--------+------+-------+------+
4 rows in set (0.000 sec)
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| class |
| dept |
| lecture |
| professor |
| student |
+----------------+
5 rows in set (0.001 sec)
MariaDB [test]> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sid | int(11) | YES | | NULL | |
| sname | varchar(16) | YES | | NULL | |
| pid | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.003 sec)
MariaDB [test]> desc lecture;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| lid | int(11) | YES | | NULL | |
| ltitle | varchar(16) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.002 sec)
MariaDB [test]> desc class;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| sid | int(11) | YES | | NULL | |
| lid | int(11) | YES | | NULL | |
| grade | float | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.003 sec)
######################################
MariaDB [test]> select * from (student a JOIN class b on (b.sid=a.sid)) JOIN lecture c on (b.lid=c.lid);
+------+-------+------+------+------+-------+------+--------+
| sid | sname | pid | sid | lid | grade | lid | ltitle |
+------+-------+------+------+------+-------+------+--------+
| 1 | Kim | 1 | 1 | 1 | 3.1 | 1 | DB |
| 1 | Kim | 1 | 1 | 2 | 4.5 | 2 | ART |
| 2 | Park | 2 | 2 | 1 | 4.3 | 1 | DB |
| 3 | Lee | 1 | 3 | 2 | 4.1 | 2 | ART |
+------+-------+------+------+------+-------+------+--------+
4 rows in set (0.001 sec)
MariaDB [test]> select a.sid, a.sname, b.grade from (student a JOIN class b on (b.sid=a.sid)) JOIN lecture c on (b.lid=c.lid);
+------+-------+-------+
| sid | sname | grade |
+------+-------+-------+
| 1 | Kim | 3.1 |
| 1 | Kim | 4.5 |
| 2 | Park | 4.3 |
| 3 | Lee | 4.1 |
+------+-------+-------+
4 rows in set (0.001 sec)
MariaDB [test]> select a.sid, a.sname, avg(b.grade) from (student a JOIN class b on (b.sid=a.sid)) JOIN lecture c on (b.lid=c.lid) group by a.sid;
+------+-------+-------------------+
| sid | sname | avg(b.grade) |
+------+-------+-------------------+
| 1 | Kim | 3.799999952316284 |
| 2 | Park | 4.300000190734863 |
| 3 | Lee | 4.099999904632568 |
+------+-------+-------------------+
3 rows in set (0.003 sec)
MariaDB [test]> select a.sid, a.sname, avg(b.grade) from (student a left JOIN class b on (b.sid=a.sid)) JOIN lecture c on (b.lid=c.lid) group by a.sid;
+------+-------+-------------------+
| sid | sname | avg(b.grade) |
+------+-------+-------------------+
| 1 | Kim | 3.799999952316284 |
| 2 | Park | 4.300000190734863 |
| 3 | Lee | 4.099999904632568 |
+------+-------+-------------------+
3 rows in set (0.000 sec)
MariaDB [test]> select a.sid, a.sname, avg(b.grade) from (student a left JOIN class b on (b.sid=a.sid)) left JOIN lecture c on (b.lid=c.lid) group by a.sid;
+------+-------+-------------------+
| sid | sname | avg(b.grade) |
+------+-------+-------------------+
| 1 | Kim | 3.799999952316284 |
| 2 | Park | 4.300000190734863 |
| 3 | Lee | 4.099999904632568 |
| 4 | Kang | NULL |
+------+-------+-------------------+
4 rows in set (0.000 sec)
MariaDB [test]> select a.sid, a.sname, avg(b.grade) from (student a left JOIN class b on (b.sid=a.sid)) left JOIN lecture c on (b.lid=c.lid) where a.sname="Kim" group by a.sid;
+------+-------+-------------------+
| sid | sname | avg(b.grade) |
+------+-------+-------------------+
| 1 | Kim | 3.799999952316284 |
+------+-------+-------------------+
1 row in set (0.000 sec)
MariaDB [test]>
'데이터베이스 SQL' 카테고리의 다른 글
기본 문법 (0) | 2021.07.29 |
---|