Last active
October 11, 2023 01:08
-
-
Save ychoi-kr/efa4062716ddbe49a8b3a3962355c442 to your computer and use it in GitHub Desktop.
SQLite로 가볍게 배우는 데이터베이스 - 연습 문제 해답
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create table "pets" ( | |
"ID" integer not null, | |
"Name" text not null, | |
"Animal" text, | |
primary key("ID") | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
insert into Person (Name, Birthday) | |
values ('지인', '1992-3-13') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
delete from Person | |
where name = '지인' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select * from person | |
where birthday is not null | |
order by birthday |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
update Person | |
set name = '유라' | |
where name like '김%' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
insert into pets (id, name, animal) | |
values | |
(1, "Dr. Harris Bonkers", "Rabbit"), | |
(2, "Moon", "Dog"), | |
(3, "Ripley", "Cat"), | |
(4, "Tom", "Cat") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ALTER TABLE Person | |
ADD COLUMN Weight INTEGER |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alter table Person | |
add column my_column |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--SQLite 3.35부터 drop column 문을 지원한다. | |
--syntax error가 발생한다면 DB Browser for SQLite 3.12.2 이상을 설치하고 다시 시도해보라. | |
alter table Person | |
drop column my_column |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create view my_view | |
as | |
select | |
name, | |
strftime('%Y', 'now') - substr(Birthday, 1, 4) - (strftime('%m-%d', 'now') < substr(Birthday, 6)) as age | |
from Person |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--null 값이 있는 행을 제외하고 계산한 평균이다. | |
--따라서 아래 계산 결과와도 일치한다. | |
select (167 + 170.3 + 164) / 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
update Person | |
set height = 166.8 | |
where name = '혜리' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select avg(height) from person |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select count(id) from pets | |
where animal = 'Cat' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select animal, count(id) from pets | |
group by animal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select animal, count(id) from pets | |
group by animal | |
having count(id) > 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--아래 질의를 수행해보면 '민아'의 생일(bdate)이 NULL이며 MM도 NULL임을 볼 수 있다. | |
select * from BirthdayView | |
--10장의 조건절을 사용한 예에서는 생일이 NULL인 '민아'를 포함한 행도 조회 결과에 포함됐지만, | |
--16장의 예(서브쿼리를 사용)에서는 '민아'를 제외한 행만 조회됐다는 차이가 있다. | |
--'MM'으로 조인을 했으므로 생일이 없는 행은 결과에 포함되지 않았다. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select | |
시도명, | |
시군구명, | |
count(*) 매장수, | |
`2022년10월_총인구수` 인구수, | |
round(1.0 * replace(`2022년10월_총인구수`, ',', '') / count(*), 1) as 매장당인구수 | |
from 상가정보 매장, 주민등록인구 인구 | |
where 인구.행정구역 like 매장.시도명 || ' ' || 매장.시군구명 || ' (%)' | |
and 시도명 in ('서울특별시', '경기도', '인천광역시') and 상호명 like '공차%' | |
group by 시도명, 시군구명 | |
order by 매장당인구수 asc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select | |
bird_name 이름, | |
substr(date_time, 1, 7) 년월, | |
count(*) 건수 | |
from bird_tracking, bird_tracking_devices | |
where bird_tracking.device_info_serial = bird_tracking_devices.device_info_serial | |
group by 이름, 년월 | |
order by 이름, 년월 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment