MySQL

내일은 SQL(응용 실전편) 실전_ch1-1-1-student&winner_list.sql

쿠카이든 2024. 5. 20. 10:34
728x90
[교재 SQL 스크립트] 실전_ch1-1-1-student&winner_list.sql
USE mydb;

create table if not exists mydb.student
(
    id     varchar(20) null,
    name   varchar(15) null,
    mobile varchar(11) null
)

insert into student (id, name, mobile)
values ('apple', '김사과', '01011110000');

create table if not exists mydb.winner_list
(
    ranking int         null,
    id      varchar(20) null
)

insert into winner_list (ranking, id)
values (1, 'strawberry');

SELECT a.ranking,
       (SELECT mobile FROM student b WHERE b.id = a.id) AS mobile
  FROM winner_list a;

SELECT a.ranking,
       (SELECT mobile FROM student b WHERE b.id = a.id) AS mobile,
       (SELECT name FROM student b WHERE b.id = a.id) AS name
  FROM winner_list a;

SELECT a.ranking,
       b.mobile,
       b.name
  FROM winner_list a
  LEFT OUTER JOIN student b
    ON a.id = b.id;
728x90