iT邦幫忙

2022 iThome 鐵人賽

DAY 17
0

SQLBolt:https://sqlbolt.com/lesson/introduction

使用語法

  • SELECT
  • WHERE

SQL Lesson 1: SELECT queries 101

Lesson 1 是我們 Day 12 介紹過的 SELECT 語法。

我們可以照右方的 step 練習指令,然後闖到下一關啦。

題目 1

-- 1. Find the title of each film
SELECT title FROM movies;

題目 2

-- 2. Find the director of each film 
SELECT director FROM movies;

題目 3

-- 3. Find the title and director of each film
SELECT title, director FROM movies;

題目 4

-- 4. Find the title and year of each film
SELECT title, year FROM movies;

題目 5

-- 5. Find all the information about each film
SELECT * FROM movies;

SQL Lesson 2: Queries with constraints (Pt. 1)

關於 WHERE,我們在 Day 13 也簡單介紹過了。

題目 1

-- 1. Find the movie with a row id of 6
SELECT * FROM movies 
WHERE id = 6;

題目 2

-- 2. Find the movies released in the years between 2000 and 2010
SELECT * FROM movies 
WHERE year 
BETWEEN 2000 AND 2010;

題目 3

-- 3. Find the movies not released in the years between 2000 and 2010
SELECT * FROM movies 
WHERE year 
NOT BETWEEN 2000 AND 2010;

題目 4

-- 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;

SQL Lesson 3: Queries with constraints (Pt. 2)

本章節是 WHERE 語句的延續。

題目 1

-- 1. Find all the Toy Story movies
SELECT * FROM movies 
WHERE title LIKE "Toy Story%";

題目 2

-- 2. Find all the movies directed by John Lasseter
SELECT * FROM movies 
WHERE director = "John Lasseter";

題目 3

-- 3. Find all the movies (and director) not directed by John Lasseter 
SELECT * FROM movies 
WHERE director != "John Lasseter";

題目 4

-- 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")

參考資料:


上一篇
Day 16 章節安排 & SQLBolt
下一篇
Day 18 SQLBolt - 2:DISTINCT, ORDER BY, LIMIT
系列文
新手小白的每天一點SQL31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言