SQL基礎の問題集
SQLの基礎構文を使った問題です。
この記事ではSQL基礎を理解することが出来る練習問題を用意しています。
プログラミング歴
この記事を書いている僕は、プログラミング歴10年以上のプログラマーです。
詳細:プログラミング未経験の新卒が10年間プログラマーとして仕事して思うこと
SQL基礎を使った問題
SQLの基礎知識を使って解くことが出来る練習問題です。ぜひ問題を解いて基礎勉強や理解に使ってください。
問題で使うデータ
SQLの練習問題で使うデータです
データベースとユーザ
-- mondaiデータベース
CREATE DATABASE mondai CHARACTER SET utf8mb4;
-- jibunというユーザ
CREATE USER jibun@'%' IDENTIFIED BY 'pasuwa-do';
-- mondaiデータベースにjibunユーザが触れるように
GRANT ALL PRIVILEGES ON mondai.* TO jibun@'%';
-- リフレッシュ
FLUSH PRIVILEGES;
会員テーブル
create table users (
id int(10) unsigned not null auto_increment comment '会員ID',
name varchar(128) not null comment '氏名',
registed datetime not null comment '登録日時',
primary key (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会員テーブル';
insert into users (name, registed) values
('マリオ', '2023-01-01 01:00:00')
,('ルイージ', '2023-02-01 02:00:00')
,('ピーチ', '2023-03-01 03:00:00')
,('クッパ', '2023-04-01 04:00:00')
,('キノピオ', '2023-05-01 05:00:00');
商品テーブル
create table products (
id int(10) unsigned not null auto_increment comment '商品ID',
name varchar(128) not null comment '名前',
numbers int(10) unsigned not null comment '在庫数',
state tinyint unsigned not null default 0 comment '状態',
registed datetime not null comment '登録日時',
primary key (id),
key numbers (numbers),
key state (state)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商品テーブル';
insert into products (name, numbers, state, registed) values
('きのこ', 25, 0 ,'2023-01-01 01:00:00')
,('フラワー', 0, 0 ,'2023-02-01 02:00:00')
,('クリボー', 5, 1 ,'2023-03-01 03:00:00')
,('スター', 10, 0 ,'2023-04-01 04:00:00');
購入履歴テーブル
create table histories (
id int(10) unsigned not null auto_increment comment '購入ID',
product_id int(10) unsigned not null comment '商品ID',
user_id int(10) unsigned not null comment '会員ID',
registed datetime not null comment '登録日時',
primary key (id),
key product_id (product_id),
key user_id (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='購入履歴テーブル';
insert into histories (product_id, user_id, registed) values
(1, 1, '2023-01-01 01:00:00')
,(2, 1, '2023-02-01 02:00:00')
,(1, 1, '2023-03-01 03:00:00');
状態マスタテーブル
create table mst_stats (
id int(10) unsigned not null auto_increment comment '状態ID',
name varchar(128) not null comment '名前',
primary key (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='状態マスターテーブル';
insert into mst_stats (name) values
('有効')
,('無効');
会員テーブル
ID | 氏名 | 登録日時 |
---|---|---|
1 | マリオ | 2023-01-01 01:00:00 |
2 | ルイージ | 2023-02-01 02:00:00 |
3 | ピーチ | 2023-03-01 03:00:00 |
4 | クッパ | 2023-04-01 04:00:00 |
5 | キノピオ | 2023-05-01 05:00:00 |
商品テーブル
ID | 名前 | 在庫数 | 状態 | 登録日時 |
---|---|---|---|---|
1 | きのこ | 25 | 0 | 2023-01-01 01:00:00 |
2 | フラワー | 0 | 0 | 2023-02-01 02:00:00 |
3 | クリボー | 5 | 1 | 2023-03-01 03:00:00 |
4 | スター | 10 | 0 | 2023-04-01 04:00:00 |
購入履歴テーブル
ID | 商品ID | 会員ID | 登録日時 |
---|---|---|---|
1 | 1 | 1 | 2023-01-01 01:00:00 |
2 | 2 | 1 | 2023-02-01 02:00:00 |
3 | 1 | 1 | 2023-03-01 03:00:00 |
状態マスターテーブル
ID | 名前 |
---|---|
1 | 有効 |
2 | 無効 |
SELECTを使った問題
問)会員テーブルの全データを表示
select * from users;
select で *(アスタリスク) を指定すると全部のカラムを取得します。
問)会員テーブルの全データを表示(氏名だけ)
select name from users;
WHEREを使った問題
問)会員テーブルからIDが3のデータを表示
select * from users where id = 3;
問)会員テーブルから登録日時が2023-04-01 00:00:00以前に登録されたデータを表示
select * from users where registed < '2023-04-01 00:00:00';
問)会員テーブルで名前が「オ」で終わるデータを表示
select * from users where name like '%オ';
ORDER BYを使った問題
問)会員テーブルを名前の昇順に並び替えて表示
select * from users order by name asc;
ディスカッション
コメント一覧
まだ、コメントがありません