Skip to main content

SQL 基礎

事前準備

本教材は以下の環境を構築して使用します

ビデオ版講義

  • SQL入門 レッスン02 CREATE DATABASE

    • ビデオ教材

    • オリジナルビデオ教材

    • コマンドプロンプトからpostgres ユーザーでログインして、データベースに接続

      • psql -U postgres を入力して postgres ユーザーのパスワードを入力します
    • ビデオで実行しているSQL文リスト

    create database tutorial2;
    • \c データベース名 でデータベースを切り替えできます
    機能MySQLPostgreSQL
    テーブル一覧SHOW TABLES;\dt
    テーブル情報の表示SHOW TABLE STATUS FROM データベース名;\d テーブル名
    テーブル作成時のSQL文を表示SHOW CREATE TABLE テーブル名;-
    • PostgreSQLは create if not exist 構文をサポートしていません
  • SQL入門 レッスン03 DROP DATABASE

    drop database tutorial2;
  • SQL入門 レッスン04 USE

    • ビデオ教材

    • オリジナルビデオ教材

    • postgreSQLはUSE 構文をサポートしていません

    • PostgreSQL では以下の手順でデータベースを表示します

      • コマンドプロンプトを開いて以下を入力します

        • psql -U postgres を入力して postgres ユーザーのパスワードを入力します

          • ログインしたら、\l と入力してテーブル一覧表示します。
          機能MySQLPostgreSQL
          データベース一覧を表示SHOW DATABSES\l
          • ビデオで実行しているコマンド
            \l
  • SQL入門 SQL入門 レッスン05 データ型

  • SQL入門 レッスン06 固定長と可変長

  • SQL入門 レッスン7 Constrain

  • SQL入門 レッスン08 CREATE TABLE

    • ビデオ教材

    • オリジナルビデオ教材

    • "user" は postgreSQLでは予約語です。予約後であるためそのままでは使用できません。使用するためにはダブルクォテーション「"」をつけて表記しなくてはいけません。毎回ダブルクォテーション「"」をつけるのは繁雑であるため user1 に変更します

    • ビデオで実行しているSQL文リスト

      create table user1(
      id int primary key,
      name varchar(255),
      email varchar(255),
      password char(30)
      )

      create table user2(
      id int,
      name varchar(255),
      email varchar(255),
      password char(30)
      )

      create table user1(
      id int primary key,
      name varchar(255)
      )
    • pgadmin から主キーをチェック

      • user2 テーブルが作成されていることを確認して をクリックします。 の Constraints の下に primary key がないのを確認します。
  • SQL入門 レッスン09 SHOW TABLES

    • ビデオ教材

    • オリジナルビデオ教材

    • ビデオで実行しているSQL文リスト

      \dt
      \d tutorial
    • コマンド比較表

      機能MySQLPostgreSQL
      テーブル一覧SHOW TABLES;\dt
      テーブル情報の表示SHOW TABLE STATUS FROM データベース名;-
      テーブル作成時のSQL文を表示SHOW CREATE TABLE テーブル名;-
    • テーブル一覧を表示するコマンドを入力

      • コマンドプロンプトからpostgres ユーザーでログインして、データベースに接続
        • psql -U postgres -d データベース名
          • データベース接続先を変えたい場合は \connect データベース名
          • \dt
    • pgadmin からでも実行できます。 SELECT * FROM pg_catalog.pg_tables;

  • SQL入門 レッスン10 DROP TABLE

    • ビデオ教材

    • オリジナルビデオ教材

    • ビデオで実行しているSQL文リスト

      create table user6(
      id int primary key,
      "u name" varchar(30)
      )

      drop table user2;
      drop table user6;
    • Postgres ではカラム名に日本語つけても文字化けしません。

    • Postgres でカラム名に日本語を含める場合には`` ではなく "" を使います。

  • SQL入門 レッスン11 ALTER TABLE

    • ビデオで実行しているSQL文リスト

      alter table user1 add address varchar(255);
      alter table user1 rename u_name to name
    • postgres でテーブル名の変更は「コマンド比較表」の通りです。

      • コマンド比較表
      機能MySQLPostgreSQL
      テーブル名の変更alter table 旧テーブル名 RENAME 新テーブル名; もしくは、 RENAME TABLE 旧テーブル名 TO 新テーブル名;alter table 旧テーブル名 rename to 新テーブル名
      列定義を変更alter table テーブル名 modify 列名 データ型;alter table テーブル名 alter column 列名 type データ型
      列名を変更alter table テーブル名 change column 旧カラム名 新カラム名 データ型ALTER TABLE user2 RENAME address3 TO address2
      複数列の削除alter table テーブル名 drop column 列名1, 列名2;alter table テーブル名 drop column 列名1, drop column 列名2
  • SQL入門 レッスン12

    • ビデオ教材

    • オリジナルビデオ教材

    • ビデオを見る前に pgadmin の query で sql 文を実行

      alter table user1 add column age integer;
    • ビデオで実行しているSQL文リスト

      insert into user1(id,name,email,password,age)
      values(2,'山田太', 'yamada@email.com',1234456,32);
      insert into user1(id, name) values(3, '田中吾郎')
    • コマンド比較表

    機能MySQLPostgreSQL
    データの入力insert into テーブル名(列名1, 列名1) values("value1", "value2")insert into テーブル名(列名1, 列名1) values (value1, value2)
    • pgadmin を起動してテーブルにデータが入力されていることを確認します。
  • SQL入門 レッスン13 SELECT文

    insert into user1(id, name, email, password, age) values(4, '大西秀宜', 'oonishi@email.com', '7845455', 24);
    • ビデオで実行しているSQL文リスト

      select id, name from user1;
      select name, email from user1;
      select email, name from user1;
      select * from user1;
      select id, name, email
      from user1;
  • SQL入門 レッスン14

    drop table user1;
    create table user1(
    id int primary key,
    name varchar(255),
    email varchar(255),
    password char(30),
    age integer,
    salary integer
    );

    insert into user1(id, name, email, password, age, salary) values(1, '鈴木一郎', 'suzuki@email.com', '123456789', 22, 5840000);
    insert into user1(id, name, email, password, age, salary) values(2, '山田太郎', 'yamada@email.com', '1234456', 32, 4200000);
    insert into user1(id, name, email, password, age, salary) values(3, '田中吾郎', 'tanaka@email.com', '6665456', 44, 3120000);
    insert into user1(id, name, email, password, age, salary) values(4, '大西秀宜', 'oonishi@email.com', '7845455', 24, 2980000);
    insert into user1(id, name, email, password, age, salary) values(5, '山村昭彦', 'yamamura@email.com', '45123345', 28, 8450000);
    insert into user1(id, name, email, password, age, salary) values(6, '村上晴彦', 'murakami@email.com', '7484541', 38, 12545000);
    insert into user1(id, name, email, password, age, salary) values(7, '小池陽子', 'koike@email.com', '4517545', 42, 6245250);
    insert into user1(id, name, email, password, age, salary) values(8, '安部沙織', 'abe@email.com', '7484555', 58, 7689250);
    insert into user1(id, name, email, password, age, salary) values(9, '前川博', 'maekawa@email.com', '495645', 29, 3178000);
    insert into user1(id, name, email, password, age, salary) values(10, '足立康史', 'adachi@email.com', '885477', 18, 4963200);
    insert into user1(id, name, email, password, age, salary) values(11, '高木弘樹', 'takagi@email.com', '965656', 39, 4921500);
  • ビデオで実行しているSQL文リスト

    select * from user1 where age > 38;
    select * from user1 where age = 38;
    select * from user1 where age <> 38;
    select * from user1 where salary <> 5840000;
    select * from user1 where salary > 5840000;
    select * from user1 where salary >= 5840000;
  • SQL入門 レッスン15

    • ビデオ教材

    • オリジナルビデオ教材

    • ビデオで実行しているSQL文リスト

      select * from user1 where age > 25 and salary > 3000000;
      select * from user1 where age > 25 and salary > 5000000;
      select * from user1 where age > 25 or salary > 5000000;
  • SQL入門 レッスン16 LIKE

    • ビデオ教材

    • オリジナルビデオ教材

    • ビデオで実行しているSQL文リスト

      select * from user1 where name like '%山%';
      select * from user1 where name like '%彦%';
      select * from user1 where name like '%___彦%';
      select * from user1 where name like '%彦%';
      select * from user1 where name like '田%';
      select * from user1 where name not like '%田%';
    • SQL 比較表

      機能MySQLPostgreSQL
      like 条件select * from user where name like "%山%"select * from user1 where name like '%山%'
  • SQL入門 レッスン17

    • ビデオ教材

    • オリジナルビデオ教材

    • ビデオで実行しているSQL文リスト

      select * from user1 where age between 28 and 48;
      select * from user1 where age not between 28 and 48;
      select * from user1 where age between 28 and 48 and salary > 5000000;
  • SQL入門 レッスン18

    • ビデオ教材

    • オリジナルビデオ教材

    • ビデオを見る前に pgadmin の query で sql 文を実行

      insert into user1(id, email, password, age, salary) values(12, 'sakaguchi@email.com', '8654794', 36, 7452000);
      insert into user1(id, name, email, password, age, salary) values(13, '大山博', 'ooyama@email.com', '4745455', 48, 4756000);
      insert into user1(id, name, email, password, age, salary) values(14, '', 'konishi@email.com', '1112233', 41, 8612000);
    • ビデオで実行しているSQL文リスト

       select * from user1 where name is null;
      select * from user1 where name = '';
      select * from user1 where name = '' or name is null
機能 | MySQL | PostgreSQL 
------------ | ------------- | -------------
name が空白を抽出 | select * from user1 where name = "" or name is null; | select * from user1 where name = '';
name が空白 or nullを抽出 | select * from user where name = "" or name is null; | select * from user1 where name = '' or name is null
  • SQL入門 レッスン19 IN

    • ビデオ教材

    • オリジナルビデオ教材

    • ビデオで実行しているSQL文リスト

       select * from user1 where age in (28, 38, 48);
      select * from user1 where age not in (28, 38, 48);
      select * from user1 where age > 30 limit 3;
  • SQL 比較表

    機能MySQLPostgreSQL
    データの入力select カラム名, ... from テーブル名 limit 開始位置, 行数;select カラム名, ... from テーブル名 limit 行数 offset 開始位置
  • SQL入門 レッスン21 DISTINCT

    insert into user1(id, name, email, password, age, salary) values(15, '大山博', 'ooyama@email.com', '4745455', 48, 4756000);
    insert into user1(id, name, email, password, age, salary) values(16, '大山博', 'ooyama@gmail.com', '4745455', 48, 4756000);
    • ビデオで実行しているSQL文リスト

       select distinct name from user1;
      select distinct name, email from user1;
  • SQL入門 レッスン22 ORDER BY

    insert into user1(id, name, email, password, age, salary) values(17, '青山孝史', 'aoyama@email.com', '8845666', 58, 8456000);
    • ビデオで実行しているSQL文リスト

       select * from user1 order by age;
      select * from user1 order by age desc;
      select * from user1 order by age desc, salary;
      select * from user1 order by age desc, salary desc;
  • SQL入門 レッスン23 AS

    CREATE TABLE user2 (
    id int primary key,
    ser_name varchar(255),
    name varchar(255),
    email varchar(255),
    password char(30),
    age integer,
    salary integer,
    branch varchar(255)
    );
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(1, '鈴木', '一郎', 'suzuki@email.com', '123456789', 22, 5840000, 'Tokyo');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(2, '山田', '太郎', 'yamada@email.com', '1234456', 32, 4200000, 'Osaka');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(3, '田中', '吾郎', 'tanaka@email.com', '6665456', 44, 3120000, 'Nagoya');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(4, '大西', '秀宜', 'oonishi@email.com', '7845455', 24, 2980000, 'Osaka');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(5, '山村', '昭彦', 'yamamura@email.com', '45123345', 28, 8450000, 'Tokyo');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(6, '村上', '晴彦', 'murakami@email.com', '7484541', 38, 12545000, 'Osaka');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(7, '小池', '陽子', 'koike@email.com', '4517545', 42, 6245250, 'Tokyo');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(8, '安部', '沙織', 'abe@email.com', '7484555', 58, 7689250, 'Tokyo');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(9, '前川', '博', 'maekawa@email.com', '495645', 29, 3178000, 'Nagoya');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(10, '足立', '康史', 'adachi@email.com', '885477', 18, 4963200, 'Tokyo');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(11, '高木', '弘樹', 'takagi@email.com', '965656', 39, 4921500, 'Osaka');
    insert into user2(id, ser_name, email, password, age, salary, branch) values(12, '', 'sakaguchi@email.com', '8654794', 36, 7452000, 'Osaka');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(13, '大山', '博', 'ooyama@email.com', '4745455', 48, 4756000, 'Tokyo');
    insert into user2(id, name, email, password, age, salary, branch) values(14, '', 'konishi@email.com', '1112233', 41, 8612000, 'Osaka');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(15, '大山', '博', 'ooyama@email.com', '4745455', 48, 4756000, 'Nagoya');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(16, '大山', '博', 'ooyama@gmail.com', '4745455', 48, 4756000, 'Osaka');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(17, '青山', '孝史', 'aoyama@email.com', '8845666', 58, 8456000, 'Tokyo');
    • ビデオで実行しているSQL文リスト

      select name from user1;
      select name as "Name" from user1;
      select salary, (salary - salary*0.1) as "NetSalary" from user1;
      select salary, (salary - salary*0.1) as "NetSalary" from user1;
      select concat(ser_name, ' ', name) as "FullName" from user2;
      select concat_ws(' ', ser_name, name) as "FullName" from user2;
      select concat_ws('+', ser_name, name) as "FullName" from user2;
    • SQL 比較表

    機能MySQLPostgreSQL
    as 句select name as Name from userselect name as "Name" from user1;
    as 句select salary, (salary - salary * 0.1) as NetSalary from user1;select salary, (salary - salary * 0.1) as "NetSalary" from user1;
    as 句select salary, (salary - salary * 0.1) as NetSalary from user1;select salary, (salary - salary * 0.1) as "NetSalary" from user1;
    as 句select concat(ser_name, ' ', name) as FullName from user2;select concat(ser_name, ' ', name) as "FullName" from user2;
    concat_wsselect concat_ws(" ", ser_name, name) as FullName from user2select concat_ws(' ', ser_name, name) as "FullName" from user2
    concat_wsselect concat_ws(" ", ser_name, name) as FullName from user2select concat_ws('+', ser_name, name) as "FullName" from user2
  • SQL入門 レッスン24 GROUP BY

    • ビデオ教材

    • オリジナルビデオ教材

    • ビデオで実行しているSQL文リスト

      select branch as branch, count(*) as number from user2 group by branch;
      select branch as branch, sum(salary) as "Salary_SUM" from user2 group by branch;
      select branch as branch, avg(salary) as "Salary_SUM" from user2 group by branch;
    • SQL 比較表

    機能MySQLPostgreSQL
    branch 事の人数を数えるselect branch as branch, count(*) as number from user2 group by branch;select branch as branch, count(*) as number from user2 group by branch;
    branch 事に合計の売上を出すselect branch as branch, sum(salary) as Salary_SUM from user2 group by branch;select branch as branch, sum(salary) as "Salary_SUM" from user2 group by branch;
    branch 事に平均の売上を出すselect branch as branch, avg(salary) as Salary_AVG from user2 group by branch;select branch as branch, avg(salary) as "Salary_AVG" from user2 group by branch;
  • SQL入門 レッスン25 DESC

  • SQL入門 レッスン26 HAVING

  • ビデオで実行しているSQL文リスト

    select branch as "Branch", avg(salary) as "Salary_AVG" from user2 group by branch
    select branch as "Branch", avg(salary) as "Salary_AVG" from user2 group by branch having avg(salary) > 5000000
  • コマンド比較表

    機能MySQLPostgreSQL
    group by を使ってグループ化select branch as Branch, avg(salary) as Salary_AVG from user2 group by branch;select branch as "Branch", avg(salary) as "Salary_AVG" from user2 group by branch
    group by と having を使ってグループ化した後の絞り込みselect branch as Branch, avg(salary) as Salary_AVG from user2 group by branch having avg(salary) > 5000000;select branch as "Branch", avg(salary) as "Salary_AVG" from user3 group by branch having avg(salary) > 5000000
  • ビデオを見る前に pgadmin の query で sql 文を実行

    CREATE TABLE user3 (
    id int primary key,
    ser_name varchar(255),
    name varchar(255),
    email varchar(255),
    password char(30),
    age integer,
    salary integer,
    branch varchar(255)
    );
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(1, '鈴木', '一郎', 'suzuki@email.com', '123456789', 22, 5840000, 'Tokyo');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(2, '山田', '太郎', 'yamada@email.com', '1234456', 32, 4200000, 'Osaka');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(3, '田中', '吾郎', 'tanaka@email.com', '6665456', 44, 3120000, 'Nagoya');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(4, '大西', '秀宜', 'oonishi@email.com', '7845455', 24, 2980000, 'Osaka');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(5, '山村', '昭彦', 'yamamura@email.com', '45123345', 28, 8450000, 'Tokyo');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(6, '村上', '晴彦', 'murakami@email.com', '7484541', 38, 12545000, 'Osaka');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(7, '小池', '陽子', 'koike@email.com', '4517545', 42, 6245250, 'Tokyo');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(8, '安部', '沙織', 'abe@email.com', '7484555', 58, 7689250, 'Tokyo');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(9, '前川', '博', 'maekawa@email.com', '495645', 29, 3178000, 'Nagoya');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(10, '足立', '康史', 'adachi@email.com', '885477', 18, 4963200, 'Tokyo');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(11, '高木', '弘樹', 'takagi@email.com', '965656', 39, 4921500, 'Osaka');
    insert into user3(id, ser_name, email, password, age, salary, branch) values(12, '', 'sakaguchi@email.com', '8654794', 36, 7452000, 'Osaka');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(13, '大山', '博', 'ooyama@email.com', '4745455', 48, 4756000, 'Tokyo');
    insert into user3(id, name, email, password, age, salary, branch) values(14, '', 'konishi@email.com', '1112233', 41, 8612000, 'Osaka');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(15, '大山', '博', 'ooyama@email.com', '4745455', 48, 4756000, 'Nagoya');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(16, '大山', '博', 'ooyama@gmail.com', '4745455', 48, 4756000, 'Osaka');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(17, '青山', '孝史', 'aoyama@email.com', '8845666', 58, 8456000, 'Tokyo');
  • ビデオで実行しているSQL文リスト

    delete from user3 where id = 9;
    delete from user3 where age > 50;
    delete from user3;
  • SQL入門 レッスン29 TRUNCATE

  • ビデオを見る前に pgadmin の query で sql 文を実行

    CREATE TABLE user3 (
    id int primary key,
    ser_name varchar(255),
    name varchar(255),
    email varchar(255),
    password char(30),
    age integer,
    salary integer,
    branch varchar(255)
    );
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(1, '鈴木', '一郎', 'suzuki@email.com', '123456789', 22, 5840000, 'Tokyo');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(2, '山田', '太郎', 'yamada@email.com', '1234456', 32, 4200000, 'Osaka');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(3, '田中', '吾郎', 'tanaka@email.com', '6665456', 44, 3120000, 'Nagoya');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(4, '大西', '秀宜', 'oonishi@email.com', '7845455', 24, 2980000, 'Osaka');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(5, '山村', '昭彦', 'yamamura@email.com', '45123345', 28, 8450000, 'Tokyo');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(6, '村上', '晴彦', 'murakami@email.com', '7484541', 38, 12545000, 'Osaka');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(7, '小池', '陽子', 'koike@email.com', '4517545', 42, 6245250, 'Tokyo');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(8, '安部', '沙織', 'abe@email.com', '7484555', 58, 7689250, 'Tokyo');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(9, '前川', '博', 'maekawa@email.com', '495645', 29, 3178000, 'Nagoya');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(10, '足立', '康史', 'adachi@email.com', '885477', 18, 4963200, 'Tokyo');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(11, '高木', '弘樹', 'takagi@email.com', '965656', 39, 4921500, 'Osaka');
    insert into user3(id, ser_name, email, password, age, salary, branch) values(12, '', 'sakaguchi@email.com', '8654794', 36, 7452000, 'Osaka');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(13, '大山', '博', 'ooyama@email.com', '4745455', 48, 4756000, 'Tokyo');
    insert into user3(id, name, email, password, age, salary, branch) values(14, '', 'konishi@email.com', '1112233', 41, 8612000, 'Osaka');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(15, '大山', '博', 'ooyama@email.com', '4745455', 48, 4756000, 'Nagoya');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(16, '大山', '博', 'ooyama@gmail.com', '4745455', 48, 4756000, 'Osaka');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(17, '青山', '孝史', 'aoyama@email.com', '8845666', 58, 8456000, 'Tokyo');
  • ビデオで実行しているSQL文リスト

    truncate table user3;

  • SQL入門 レッスン30 他のテーブルデータを追加

      drop table user2;
    CREATE TABLE user3 (
    id int Primary key,
    ser_name varchar(255),
    name varchar(255),
    email varchar(255),
    password char(30),
    age integer,
    salary integer,
    branch varchar(255)
    );
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(201, '鈴木', '一郎', 'suzuki@email.com', '123456789', 23, 5840000, 'Tokyo');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(202, '山田', '太郎', 'yamada@email.com', '1234456', 35, 4200000, 'Osaka');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(203, '田中', '吾郎', 'tanaka@email.com', '6665456', 45, 3120000, 'Nagoya');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(204, '大西', '秀宜', 'oonishi@email.com', '7845455', 25, 2980000, 'Osaka');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(205, '山村', '昭彦', 'yamamura@email.com', '45123345', 29, 8450000, 'Tokyo');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(206, '村上', '晴彦', 'murakami@email.com', '7484541', 39, 12545000, 'Osaka');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(207, '小池', '陽子', 'koike@email.com', '4517545', 43, 6245250, 'Tokyo');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(208, '安部', '沙織', 'abe@email.com', '7484555', 59, 7689250, 'Tokyo');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(209, '前川', '博', 'maekawa@email.com', '495645', 30, 3178000, 'Nagoya');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(210, '足立', '康史', 'adachi@email.com', '885477', 19, 4963200, 'Tokyo');
    insert into "user3"(id, ser_name, name, email, password, age, salary, branch) values(211, '高木', '弘樹', 'takagi@email.com', '965656', 40, 4921500, 'Osaka');
    insert into user3(id, ser_name, email, password, age, salary, branch) values(212, '', 'sakaguchi@email.com', '8654794', 37, 7452000, 'Osaka');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(213, '大山', '博', 'ooyama@email.com', '4745455', 49, 4756000, 'Tokyo');
    insert into user3(id, name, email, password, age, salary, branch) values(214, '', 'konishi@email.com', '1112233', 42, 8612000, 'Osaka');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(215, '大山', '博', 'ooyama@email.com', '4745455', 49, 4756000, 'Nagoya');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(216, '大山', '博', 'ooyama@gmail.com', '4745455', 49, 4756000, 'Osaka');
    insert into user3(id, ser_name, name, email, password, age, salary, branch) values(217, '青山', '孝史', 'aoyama@email.com', '8845666', 59, 8456000, 'Tokyo');
    CREATE TABLE user2 (
    id int Primary key,
    ser_name varchar(255),
    name varchar(255),
    email varchar(255),
    password char(30),
    age integer,
    salary integer,
    branch varchar(255)
    );
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(101, '鈴木', '一郎', 'suzuki@email.com', '123456789', 23, 5840000, 'Tokyo');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(102, '山田', '太郎', 'yamada@email.com', '1234456', 35, 4200000, 'Osaka');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(103, '田中', '吾郎', 'tanaka@email.com', '6665456', 45, 3120000, 'Nagoya');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(104, '大西', '秀宜', 'oonishi@email.com', '7845455', 25, 2980000, 'Osaka');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(105, '山村', '昭彦', 'yamamura@email.com', '45123345', 29, 8450000, 'Tokyo');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(106, '村上', '晴彦', 'murakami@email.com', '7484541', 39, 12545000, 'Osaka');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(107, '小池', '陽子', 'koike@email.com', '4517545', 43, 6245250, 'Tokyo');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(108, '安部', '沙織', 'abe@email.com', '7484555', 59, 7689250, 'Tokyo');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(109, '前川', '博', 'maekawa@email.com', '495645', 30, 3178000, 'Nagoya');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(110, '足立', '康史', 'adachi@email.com', '885477', 19, 4963200, 'Tokyo');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(111, '高木', '弘樹', 'takagi@email.com', '965656', 40, 4921500, 'Osaka');
    insert into user2(id, ser_name, email, password, age, salary, branch) values(112, '', 'sakaguchi@email.com', '8654794', 37, 7452000, 'Osaka');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(113, '大山', '博', 'ooyama@email.com', '4745455', 49, 4756000, 'Tokyo');
    insert into user2(id, name, email, password, age, salary, branch) values(114, '', 'konishi@email.com', '1112233', 42, 8612000, 'Osaka');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(115, '大山', '博', 'ooyama@email.com', '4745455', 49, 4756000, 'Nagoya');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(116, '大山', '博', 'ooyama@gmail.com', '4745455', 49, 4756000, 'Osaka');
    insert into user2(id, ser_name, name, email, password, age, salary, branch) values(117, '青山', '孝史', 'aoyama@email.com', '8845666', 59, 8456000, 'Tokyo');
    • ビデオで実行しているSQL文リスト

      insert into user2(id, name, email) select id, name, email from user3;
  • SQL入門 レッスン31 CREATE SELECT

    • ビデオ教材

    • オリジナルビデオ教材

    • コマンド比較表

      機能MySQLPostgreSQL
      テーブルを作成し算出されるデータを格納しますcreate table user4 select * from user3 where salary > 5000000create table user4 as select * from user3 where salary > 4000000;
      • PostgreSQL は DESCRIBE をサポートしていません。
    • ビデオで実行しているSQL文リスト

      create table user4 as select * from user3 where salary > 4000000;
  • SQL入門 レッスン32 CREATE LIKE

  • SQL入門 レッスン33 数値関数

    • ビデオ教材

    • オリジナルビデオ教材

    • ビデオで実行しているSQL文リスト

      select abs(5123);
      select floor(5123.235);
      select truncate(5123.935, 2);
      select truncate(5123.935, 4);
      select div(4,2);
      select div(456,4);
      select mod(889,2);
      select mod(888,2);
    • コマンド比較表

      機能MySQLPostgreSQL
      テーブルを作成し算出されるデータを格納しますselect truncate(5123.93545, 4);select trunc(5123.935, 2);
      割り算select 4 div 2;select div(4,2);
      剰余select 889 mod 2select mod(889,2);
  • SQL入門 レッスン34 数値関数2

  • SQL入門 レッスン35 文字関数

    • ビデオ教材

    • オリジナルビデオ教材

    • ビデオで実行しているSQL文リスト

      select ser_name, name, random() as random from user3;
      select ser_name, name from user3;
      select concat(ser_name, ' ', name) as "FullName" from user3;
      select upper('user1');
      select lower('user1');
      select lower('user');
      機能MySQLPostgreSQL
      ランダムに番号を表示select ser_name, name, rand() as random from user3;select ser_name, name, random() as random from user3;
      concat を使った文字の連結select concat(ser_name, " ", name) as FullName from user3;select concat(ser_name, ' ', name) as "FullName" from user3;
      区切り文字を指定select concat_ws(" ",ser_name, name) as FullName from user3select concat_ws(' ',ser_name, name) as "FullName" from user3
      英文字列を大文字に変換select upper("user")select upper('user1');
      英文字列を小文字に変換select lower("user")select lower('user1');
  • SQL入門 レッスン36 文字関数2

  • SQL入門 レッスン37 文字関数3

  • SQL入門 レッスン38 文字関数4

  • SQL入門 レッスン39 日付関数

    • ビデオ教材

    • オリジナルビデオ教材

    • ビデオで実行しているSQL文リスト

      select current_date;
      select current_time;
      select current_date + INTERVAL '10 day';
      機能MySQLPostgreSQL
      今日の日付を表示select curtime();select current_date;
      今の時間を表示select curtime();select current_time;
      指定された時間間隔を現在の日付から加算select adddate(curdate(),10);select current_date + INTERVAL '10 day';
      指定された時間間隔を指定した日付から加算select adddate("2017-5-2", 150);-
  • SQL入門 レッスン40 日付関数2

  • SQL入門 レッスン41 日付関数3

  • SQL入門 レッスン42 集計関数

    • ビデオ教材

    • オリジナルビデオ教材

    • ビデオで実行しているSQL文リスト

      select sum(salary) as "Salary_total" from user3;
      select branch, sum(salary) as "Salary_total" from user3 group by branch;
      select branch, avg(salary) as "Salary_AVG" from user3 group by branch;
      select branch, count(*) as "Number" from user3 group by branch;
      select max(salary) from user3;
      select min(salary) from user3;
      select ser_name, name, salary from user3 order by salary desc limit 5;
      select ser_name, name, salary from user3 order by salary limit 5;
  • SQL入門 レッスン43 ビュー概要

  • ビデオで実行しているSQL文リスト

    create view user_view as select ser_name, name, email from user3;
    select * from user_view;
  • SQL入門 レッスン44 ビューの操作

    • ビデオ教材

    • オリジナルビデオ教材

    • ビデオで実行しているSQL文リスト

      drop view user_view;
      create view user_name as select ser_name, name, email, age from user3;
    • コマンド比較表

      機能MySQLPostgreSQL
      view の再定義alter view user_view as select ser_name, name, email, age from user3 where branch = 'Osaka'drop view user_name; create view user_name as select ser_name, name, email, age from user3 where branch = 'Osaka';
  • SQL入門 レッスン45 JOIN

    CREATE TABLE party (
    id int Primary key,
    name varchar(255)
    );
    insert into party(id, name) values(1, 'A政党');
    insert into party(id, name) values(2, 'B政党');
    insert into party(id, name) values(3, 'C政党');
    insert into party(id, name) values(4, 'D政党');
    CREATE TABLE candidate (
    id int Primary key,
    party_id integer,
    name varchar(255)
    );
    insert into candidate(id, party_id, name) values(1,1,'Suzuki');
    insert into candidate(id, party_id, name) values(2,2,'Yamada');
    insert into candidate(id, party_id, name) values(3,1,'Sasaki');
    insert into candidate(id, party_id, name) values(4,2,'Gtoh');
    insert into candidate(id, party_id, name) values(5,5,'Yamamoto');
    insert into candidate(id, party_id, name) values(6,3,'Watanabe');
    • ビデオで実行しているSQL文リスト
      select c.name, p.name from candidate as c join party as p on c.party_id = p.id;
      select c.name, p.name from candidate as c inner join party as p on c.party_id = p.id;
  • SQL入門 レッスン46 WHERE JOIN

  • SQL入門 レッスン47 複数JOIN

    • ビデオ教材

    • オリジナルビデオ教材

    • ビデオで実行しているSQL文リスト

      alter table candidate add area_id int;
      create table area(
      id int primary key,
      name varchar(255)
      );
      insert into area(id, name) values(1,'東京1区'),(2,'神奈川2区'),(3,'大阪3区'),(4,'兵庫1区'),(5,'福岡3区'),(6,'新潟1区')
      select c.name, p.name, a.name from candidate as c join party as p on p.id = c.party_id join area as a on a.id = c.area_id;
    • コマンド比較表

      機能MySQLPostgreSQL
      area テーブルに選挙区のデータを入力insert into area(id, name) values(1,"東京1区"),(2,"神奈川2区"),(3,"大阪3区"),(4,"兵庫1区"),(5,"福岡3区"),(6,"新潟1区");insert into area(id, name) values(1,'東京1区'),(2,'神奈川2区'),(3,'大阪3区'),(4,'兵庫1区'),(5,'福岡3区'),(6,'新潟1区')
    • area_idに直接データを入れる作業

      • candidate テーブルを右クリック → View/Edit Data → All Rows
      • をクリックして保存します。
  • SQL入門 レッスン48 LEFT JOIN

    • ビデオ教材

    • オリジナルビデオ教材

    • ビデオで実行しているSQL文リスト

      select c.name, p.name from candidate as c left join party as p on p.id = c.party_id;
      select c.name, p.name from candidate as c left outer join party as p on p.id = c.party_id;
  • SQL入門 レッスン49 RIGHT JOIN

    • ビデオ教材

    • オリジナルビデオ教材

    • ビデオで実行しているSQL文リスト

      select c.name, p.name from candidate as c right join party as p on p.id = c.party_id;
      select c.name, p.name from candidate as c right outer join party as p on p.id = c.party_id;
  • SQL入門 レッスン50 FULL JOIN

    • ビデオ教材

    • オリジナルビデオ教材

    • ビデオで実行しているSQL文リスト

      select c.name, p.name from candidate as c full outer join party as p on p.id = c.party_id;
    • コマンド比較表

      機能MySQLPostgreSQL
      FULL OUTER JOINないselect c.name, p.name from candidate as c full outer join party as p on p.id = c.party_id
  • SQL入門 レッスン51 トリガー概要

  • SQL入門 レッスン52 トリガー操作

  • SQL入門 レッスン53 サブクエリー

    • ビデオ教材

    • オリジナルビデオ教材

    • ビデオで実行しているSQL文リスト

      select salary from user3;
      select max(salary) from user3;
      select * from user3 where salary = (select max(salary) from user3);
      select * from user3 where salary > (select avg(salary) from user3);
  • SQL入門 レッスン54 文字コード

  • SQL入門 レッスン55 照合順序