SQL練習問題

プログラミング
ColossusCloud / Pixabay
この記事は約3分で読めます。

01. 以下のデータベース/テーブルを作成せよ。

データベース名:test_database

テーブル名:test_usertable

UserIdUserNameUserAge
1鈴木21
2佐藤25
3田中18
4山田24
6戦場ヶ原17

02 .田中さんの歳を19歳に変更せよ。

03 .戦場ヶ原さんを削除せよ。

04. 生年月日のカラム(カラム名:Birth)を追加せよ。

05. 以下のようにbirthdayカラムを作成し生年月日を追加せよ。

鈴木:1992-12-2
佐藤:1988-3-9
田中:1995-4-27
山田:1989-9-29

06. 歳が若い順に、名前を表示せよ。

07. 生年月日が1月から6月の人の名前を表示せよ。

08. レコードの数を数えよ。

09. 登録してあるユーザの歳の平均を表示せよ。

10. ユーザ名が”山”で始まるレコードを表示せよ。

模範解答-postgres編-

# 01 (データベース/テーブルの作成/レコードの挿入)
create table test_usertable(user_id integer, user_name text, user_age integer);
insert into test_usertable(user_id, user_name, user_age) values(2,'佐藤',25);
select * from test_username;

# 02 (レコードの更新/where句)
update test_usertable set user_age=19 where user_name ='田中';

# 03 (レコードの削除)
delete from test_usertable where user_name = '戦場ヶ原';

# 04 (カラムの追加)
alter table test_usertable add birthday date;
(削除:alter table test_usertable drop birthday;)

# 05 (レコードの更新/where句)
update test_usertable set birthday = '1992-12-2' where user_name='鈴木';

# 06 (並び替え,ソート)
select user_name from test_usertable order by user_age;
(降順:select user_name from test_usertable order by user_age desc;)

# 07 (where句/date_part関数)
select * from test_usertable where date_part('month',birthday) >=1 and date_part('month',birthday)<=6;

# 08 (count関数)
select count(*) FROM test_usertable;

# 09 (avg関数)
select avg(user_age) from test_usertable;

# 10 (like検索/パターンマッチング)
select user_name from test_usertable where user_name like '山%';

コメント

  1. […] ★あとは、一般的なSQL操作の練習あるのみ! SQL練習問題 […]

タイトルとURLをコピーしました