SQLBolt:https://sqlbolt.com/lesson/introduction
Lesson 1 是我們 Day 12 介紹過的 SELECT
語法。
我們可以照右方的 step 練習指令,然後闖到下一關啦。
-- 1. Find the title of each film
SELECT title FROM movies;
-- 2. Find the director of each film
SELECT director FROM movies;
-- 3. Find the title and director of each film
SELECT title, director FROM movies;
-- 4. Find the title and year of each film
SELECT title, year FROM movies;
-- 5. Find all the information about each film
SELECT * FROM movies;
關於 WHERE
,我們在 Day 13 也簡單介紹過了。
-- 1. Find the movie with a row id of 6
SELECT * FROM movies
WHERE id = 6;
-- 2. Find the movies released in the years between 2000 and 2010
SELECT * FROM movies
WHERE year
BETWEEN 2000 AND 2010;
-- 3. Find the movies not released in the years between 2000 and 2010
SELECT * FROM movies
WHERE year
NOT BETWEEN 2000 AND 2010;
-- 4. Find the first 5 Pixar movies and their release year
-- 網站解法
SELECT title, year FROM movies
WHERE year <= 2003;
-- 筆者解法:
SELECT * FROM movies
ORDER BY year
LIMIT 5;
本章節是 WHERE
語句的延續。
-- 1. Find all the Toy Story movies
SELECT * FROM movies
WHERE title LIKE "Toy Story%";
-- 2. Find all the movies directed by John Lasseter
SELECT * FROM movies
WHERE director = "John Lasseter";
-- 3. Find all the movies (and director) not directed by John Lasseter
SELECT * FROM movies
WHERE director != "John Lasseter";
-- 4. Find all the WALL-* movies
SELECT * FROM movies
WHERE title LIKE "WALL-%";
我們在 Day 13 有列出可使用的運算符,SQLBolt 則有整理出常用的運算符:
運算符 | 說明 | 範例 |
---|---|---|
= | 等於 | col_name = "abc" |
!= or <> |
不等於 | col_name != "abcd" |
LIKE | 模糊範圍等式 | col_name LIKE "ABC" |
NOT LIKE | 模糊範圍不等式 | col_name NOT LIKE "ABCD" |
% | 匹配單一或多個字符(僅適用 LIKE or NOT LIKE ) |
col_name LIKE "%AT%" |
- | 匹配單一字符(僅適用 LIKE or NOT LIKE ) |
col_name LIKE “AN_” |
IN (…) | 匹配完全相同的字串 | col_name IN ("A", "B", "C") |
NOT IN (…) | 排除完全相同的字串 | col_name NOT IN ("D", "E", "F") |
參考資料: