
select name from instructor where salary between 900000 and 1000000;
select name, course_id
from instructor,teaches
where(instructor.ID,dept_name)=(teaches.ID,'Biology');
delete from instructor;
delete from instructoer
where dept_name='Finance';
delete from instructor
where dept_name in(select dept_name
from department
where building='AI building');
update instructor
set salary = salary * 1.03
where salary > 100000;
update medical
set bmi = 40
where bmi > 40;
select * from instructor, teaches;
select distinct T.name
from instructors as T, instructors as S
where T.salary > S.salary and S.dept_name='Com.Sci';
Percent (%) - matches any substring
underscore (_) - matches any single char
select name from instructor where name like '%dar%';
select name from instructor where name like 'm_d%';
select number from table_name where condition like '100\%' escape '\';
select concat(LastName,',',FristName) as Name from Person
/* Jason | doe -> Jason, doe */
select UPPER('louder please');
select lower('AABB');
select length("SQL Tutorial") as LengthOfString; //return the length of string
select substring("MySqltest",1,5);
//search the substring and select 1 to 5
select distinct name from instructor order by name;
select distinct name from instructor order by name desc;
์ค๋ณต ์ ๊ฑฐ๋ ๋์ง ์๋๋ค. ๋ฐ๋ก distinct ํด์ค์ผํ๋ค.
(select course_id from section where sem='Fall' and year= 2009)
union (select course_id from section where sem='Spring' and year=2010);
(select course_id from section where sem='Fall' and year=2009)
intersect
(select course_id from section where sem='Spring' and year=2010);
(select course_id from section where sem='Fall' and year=2009)
except
(select course_id from section where sem='Spring' and year=2010);
select avg(salary) from instructor where dept_name='Comp.Sci';
select count(distinct ID)
from teaches
where semester='Spring' and year=3020;
select count(*) from course;
select avg(bmi) from health where sex='male';
select count(smoker) from smoke where IsSmoke='yes' and smoke_child >= 2;
select min(age) from smoke where IsSmoke = 'yes';
'group by + column name' ์ผ ๋, column name์ด ์์ select ์๋ ๊ผญ ์์ด์ผํ๋ค.
ex) select name ... group by name;
select dept_name, avg(salary) as avg_salary
from instructor group by dept_name;
Where ๋ group by ๋ ์๊ด ์๋ค. group by ์ด์ ์ ์กฐ๊ฑด๋ฌธ
Having ์ group by ์ดํ์ group์ ํ์ํ ์กฐ๊ฑด๋ฌธ
select dept_name, avg(salary)
from instructor
group by dept_name having salary > 42000;
select dept_name, avg(salary)
from instructor
where salary > 30000
group by dept_name having salary > 42000;
(1) select sum(salary) from instructor; //not contain null
(2) select sum(*) from instructor; //contain null
select smoker, count(*) from medical group by smoker;
select smoker,count(*) from medical where age >= 40 group by smoker;
select Children, count(*) as cnt
from medical
group by Children having cnt >= 5;
select Children, count(*) as cnt
from medical
where Children >= 1
group by children having cnt >= 5;
๐ Region๋ณ๋ก ๋ด๋ฐฐ ํผ๋ ์ฌ๋๋ค์ ๊ทธ๋ฃนํํ์ฌ ๊ทธ๋ฃน๋ด ๋ฐ์ดํฐ ๊ฐ์๋ฅผ ๊ฐ๋ ค์จ ํ,
๋ฐ์ดํฐ ๊ฐ์์ ๋ํด ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ๋ก ์กฐํ
select Region, count(*) as cnt
from medical
where smoker='yes'
group by Region order by cnt desc;
select type, count(*) from hero_collection group by type;
select type, count(name)
from hero_collection
where type >= 2
group by type
select type, count(name) as cnt
from hero_collection
group by type having cnt >= 2;
select type, count(name) as cnt
from hero_collection
where type >= 2
group by type having cnt >= 2;
๐ type์ด 2์ด์์ธ ์ฌ๋๋ค์ ๊ทธ๋ฃนํํ์ฌ ๊ทธ๋ฃน๋ด ๋ฐ์ดํฐ ๊ฐ์๋ฅผ ๊ฐ์ ธ์จ ํ, ๊ทธ ์ค์ ์๊ฐ 2๊ฐ ์ด์์ธ ๋ฐ์ดํฐ๋ฅผ type ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ๋ก ์กฐํ
select type, count(name) as cnt
from hero_collection
where type >= 2
group by type having cnt >= 2
order by type decs;
IN : ๋ฆฌํด๋๋ ๊ฐ ์ค์์ ์กฐ๊ฑด์ ํด๋นํ๋ ๊ฐ์ด ์์ผ๋ฉด ์ฐธ
ANY, SOME : ์๋ธ์ฟผ๋ฆฌ์ ์ํด ๋ฆฌํด๋๋ ๊ฐ๊ฐ์ ๊ฐ๊ณผ ์กฐ๊ฑด์ ๋น๊ตํ์ฌ ํ๋ ์ด์์ ๋ง์กฑ์ํค๋ฉด ์ฐธ
ALL : ์๋ธ์ฟผ๋ฆฌ์ ์ํด ๋ฆฌํด๋๋ ๋ชจ๋ ๊ฐ๊ณผ ์กฐ๊ฑด๊ฐ์ ๋น๊ตํ์ฌ ๋ชจ๋ ๊ฐ์ ๋ง์กฑํด์ผ๋ง ์ฐธ
[1]select * from t1 where col=[2](select col1 from t2);
//[1] is outer query(or outer statement)
//[2] is subquery
[1] select * from medical
where age=(select min(age) from medical);
[2] select * from medical where age=18;
select min(age) as age from medical
union select max(age) from medical
// the column name will be age
select min(age) from medical
union select max(age) as age from medical
//the column name will be min(age) because union follows the first column's name
select *
from medical
where age in (select min(age) from medical
union select max(age) from medical);
select * from medical
where age = (select min(age) from medical);
// '=' can use only the value is one thing
select distinct course_id from section
where semester='Fall' and year=2022
and course_id
in (select course_id from section where semester = 'Spring' and year = 2023);
select distinct course_id from section
where semester='Fall'
and year=2022
and course_if in
(select course_id from section
where semester = 'Spring' and year = 2023
and course_id in
(select course_id from section
where semester = 'Fall' and year = 2023));
// both are same meaning
select distinct course_id from section
where semester = 'Fall'
and year = 2022
and course_id in(
select course_id from section
where semester = 'Spring' and year = 2023)
and course_id in(
select course_id from section
where semester = 'Fall' and year = 2023);
select count(distinct ID) from takes
where (course_id, sec_id,semester,year)
in(select course_id, sec_id, semester,year)
from teaches
where ID=10101);
null์ () ์์ ๋ค์ด๊ฐ๋ฉด ์๋๊ณ is null, is not null๋ง ๊ฐ๋ฅํจ
ex) ์๋ ์์ ๊ฐ์ ๊ฒฝ์ฐ๋ ์๋จselect * from food_table as f where f.number not in(7,8,9,10,null)
select * from food_table f where f.number in(1,2,3,4,5,6);
//search 1,2,3,4,5,6 from food_table
select * from food_table as f where f.number not in(7,8,9,10)
and f.number is not null
//search the numbers execpt 7,8,9,10 and null
any(some) = min
all = max
select distinct T.name
from instructor as T, instructor as S
where T.salary > S.salary and S.dept_name='Biology';
[1] select name
from instructor
where salary > some (select salary from instructor
where dept_name='Biology');
[2] select name
from instructor
where salary > (select min(salary) from instructor
where dept_name='Biology');
select name
from instructor
where salary > all (select salary from instructor
where dept_name='Biology');
IN ๊ณผ ์ ์ฌํ๋ค.
sub query์ ๋ฐํ๊ฐ์ด ์์ผ๋ฉด true, ์์ผ๋ฉด flase
select *from customers
where exists(select * from orders where orders.c_id=customers.c_id);
select *from customers
where not exists(select * from orders where orders.c_id=customers.c_id);