[プログラマー]-レベル2 MySQL


質問する
リンク
に答える
1.猫と犬が何匹いるか
SELECT ANIMAL_TYPE, COUNT(ANIMAL_TYPE) AS count 
FROM ANIMAL_INS 
GROUP BY ANIMAL_TYPE 
ORDER BY ANIMAL_TYPE;
2.ルーシーとエラを探す
SELECT ANIMAL_ID, NAME, SEX_UPON_INTAKE 
FROM ANIMAL_INS 
WHERE NAME 
IN ('Lucy', 'Ella', 'Pickle', 'Rogan', 'Sabrina', 'Mitty')
ORDER BY ANIMAL_ID;
3.最高価格を求める
SELECT MIN(DATETIME) FROM ANIMAL_INS;
4.同名動物を探す
SELECT NAME, COUNT(NAME) AS COUNT 
FROM ANIMAL_INS
GROUP BY NAME
HAVING COUNT(NAME) > 1
ORDER BY NAME;
5.elという名前の動物を探す
SELECT ANIMAL_ID, NAME 
FROM ANIMAL_INS 
WHERE NAME LIKE '%el%' AND ANIMAL_TYPE = 'Dog' 
ORDER BY NAME;
6.動物の数を得る
SELECT COUNT(ANIMAL_ID) AS count FROM ANIMAL_INS;
7.養子縁組を探す(1)
SELECT date_format(DATETIME, '%k') AS HOUR, COUNT(date_format(DATETIME, '%k')) AS COUNT
FROM ANIMAL_OUTS
GROUP BY HOUR
HAVING(HOUR > 8) AND (HOUR < 20)
ORDER BY HOUR * 1;
8.処理NULL
SELECT ANIMAL_TYPE, IFNULL(NAME, 'No name') AS NAME, SEX_UPON_INTAKE
FROM ANIMAL_INS
ORDER BY ANIMAL_ID;
9.中性化の有無を知る
SELECT ANIMAL_ID, NAME, 
CASE 
    WHEN SEX_UPON_INTAKE LIKE 'Neutered%' THEN 'O'
    WHEN SEX_UPON_INTAKE LIKE 'Spayed%' THEN 'O'
    ELSE 'X'
END AS '중성화'
FROM ANIMAL_INS
ORDER BY ANIMAL_ID;
10.重複除外
SELECT COUNT(DISTINCT NAME) AS count FROM ANIMAL_INS;
11.DATETIMEからDATEへの変換
SELECT ANIMAL_ID, NAME, date_format(DATETIME, '%Y-%m-%d') AS '날짜'
FROM ANIMAL_INS
ORDER BY ANIMAL_ID;