天天看點

在sqlbolt上學習SQL

該網站能夠學習sql基礎,并且能在網頁中直接輸入sql語句進行查詢。

學習網站原網址https://sqlbolt.com/ (!部分指令該網站不支援,且存在一些bug!)

該文為SQLBolt學習者提供答案參考

查詢表格中的特定列

查詢所有列

在sqlbolt上學習SQL

1.Find the title of each film

從Movies中找到每個電影的名字(Title)

2.Find the director of each film

找到每個電影的Director

3.Find the title and director of each film

找到每個電影的Title和Director

4.Find the title and year of each film

找到每個電影的Title和Year

5.Find all the information about each film

找到每個電影的所有資訊

帶限制的選擇查詢

Operator

Condition

SQL Example

=, !=, < <=, >, >=

标準數值運算符

列 != 4

BETWEEN … AND …

數值在兩個值範圍内 (包含)

列 BETWEEN 1.5 AND 10.5

NOT BETWEEN … AND …

數值不在兩個值範圍内 (包含)

列 NOT BETWEEN 1 AND 10

IN (…)

數值在清單中

列 IN (2, 4, 6)

NOT IN (…)

數值沒在清單中

列 NOT IN (1, 3, 5)

在sqlbolt上學習SQL

1.Find the movie with a row id of 6

找到Id為6的電影

2.Find the movies released in the years between 2000 and 2010

找到movies中2000到2010年的電影

3.Find the movies not released in the years between 2000 and 2010

找到movies中沒在2000到2010年的電影

4.Find the first 5 Pixar movies and their release year

找到最早的5個皮克斯電影和它們的年份

Example

=

區分大小寫的字元串精确比較 (注意單個的相等)

列 = "abc"

!= or <>

區分大小寫的字元串不等精确比較

列 != "abcd"

LIKE

不區分大小寫的精确字元串比較

列 LIKE "ABC"

NOT LIKE

不區分大小寫的精确字元串不等比較

列 NOT LIKE "ABCD"

%

用于字元串中的任意位置,以比對由零個或多個字元組成的序列 (隻和 LIKE 或 NOT LIKE 同時使用)

列 LIKE "%AT%" (matches "AT", "ATTIC", "CAT" or even "BATS")

_

用于字元串中的任意位置比對單個字元 (隻和 LIKE 或 NOT LIKE 同時使用)

列 LIKE "AN_" (matches "AND", but not "AN")

字元串存在于清單中

列 IN ("A", "B", "C")

字元串不在清單中

列 NOT IN ("D", "E", "F")

在sqlbolt上學習SQL

1.Find all the Toy Story movies

找到所有的Toy Story電影

2.Find all the movies directed by John Lasseter

找到所有由John Lasseter導演的電影

3.Find all the movies (and director) not directed by John Lasseter

找到所有的不是John Lasseter導演的不是電影(和Director)

4.Find all the WALL-* movies

找到所有的WALL-*電影

唯一結果的選擇查詢

帶有有序結果的選擇查詢

限制行的選擇查詢

在sqlbolt上學習SQL

1.List all directors of Pixar movies (alphabetically), without duplicates

列出所有的皮克斯電影的directors(照字母順序排列),沒有重複

2.List the last four Pixar movies released (ordered from most recent to least)

列出最近上映的四部皮克斯電影(從最近源到最早)

3.List the first five Pixar movies sorted alphabetically

按字母順序列出皮克斯的前五部電影

4.List the next five Pixar movies sorted alphabetically

按字母順序列出皮克斯5到10部電影

選擇查詢

在sqlbolt上學習SQL

1.List all the Canadian cities and their populations

列出所有加拿大城市(City)及其人口(Population)

2.Order all the cities in the United States by their latitude from north to south

按照從北到南的緯度把美國所有的城市排序

3.List all the cities west of Chicago, ordered from west to east

列出芝加哥以西的所有城市,按從西到東的順序

4.List the two largest cities in Mexico (by population)

列出墨西哥最大的兩個城市(按人口分)

5.List the third and fourth largest cities (by population) in the United States and their population

列出美國第三和第四大城市(按人口)及其人口

注:Latitude 緯度, Longitude 經度

在多個表上使用INNER JOIN選擇查詢

在sqlbolt上學習SQL

1.Find the domestic and international sales for each movie

找出每部電影(Title)在國内和國際上的銷售情況(Domestic_sales,International_sales)

2.Show the sales numbers for each movie that did better internationally rather than domestically

展示每一部在國際(International_sales)上比在國内(Domestic_sales)銷量更好的電影

3.List all the movies by their ratings in descending order

把所有的電影按評分(Rating)降序排列

在多個表上使用左/右/全連接配接選擇查詢

在sqlbolt上學習SQL

1.Find the list of all buildings that have employees

找到所有有員工的建築清單

2.Find the list of all buildings and their capacity

找到所有建築及其Capacity的清單

3.List all buildings and the distinct employee roles in each building (including empty buildings)

列出所有建築和每個建築中不同的員工職責(role)(包括空的建築)

對NULL值有限制的選擇查詢

在sqlbolt上學習SQL

1.Find the name and role of all employees who have not been assigned to a building

查找所有尚未配置設定到大樓的員工的名字和職責

2.Find the names of the buildings that hold no employees

找出沒有員工的大樓的名字

帶有表達式的查詢示例

帶表達式的别名選擇查詢

具有列和表别名的查詢示例

在sqlbolt上學習SQL

1.List all movies and their combined sales in millions of dollars

列出所有電影及其總銷售額(以百萬美元計)

2.List all movies and their ratings in percent

列出所有電影及其收視率(使用百分比)

3.List all movies that were released on even number years

列出所有偶數年發行的電影

Select query with aggregate functions over all rows

Function

Description

COUNT(), COUNT(column*)

一個常用函數,用于在未指定列名的情況下統計組中的行數。否則,計算指定列中具有非空值的組中的行數。

MIN(column)

為組中的所有行查找指定列中的最小數值

MAX(column)

為組中的所有行在指定列中查找最大的數值。

AVG(column)

在指定列中查找組中所有行的平均數值。

SUM(column)

在指定的列中查找組中各行的所有數值之和。

在sqlbolt上學習SQL

1.Find the longest time that an employee has been at the studio

找出員工在工作室工作的最長時間

2.For each role, find the average number of years employed by employees in that role

對于每個角色,查找該角色雇員的平均工作年限

3.Find the total number of employee years worked in each building

查找在每個建築物工作的員工總年數

選擇具有限制的查詢

在sqlbolt上學習SQL

1.Find the number of Artists in the studio (without a HAVING clause)

查找工作室中藝術家的數量(不用 HAVING 子句)

2.Find the number of Employees of each role in the studio

查找工作室中每種職責的雇員人數

3.Find the total number of years employed by all Engineers

找到所有工程師(Engineer)的總受雇年數(Years_employed)

完整的SELECT查詢

在sqlbolt上學習SQL

1.Find the number of movies each director has directed

找出每位導演導演的電影數量

2.Find the total domestic and international sales that can be attributed to each director

找出每一位導演在國内和國際上的總銷售額

包含所有列的值的插入語句

插入特定列的語句

在sqlbolt上學習SQL

1.Add the studio’s new production, Toy Story 4 to the list of movies (you can use any director)

将該工作室的新作品Toy Story 4添加到電影清單中(你可以使用任何導演)

2.Toy Story 4 has been released to critical acclaim! It had a rating of 8.7, and made 340 million domestically and 270 million internationally. Add the record to the BoxOffice table.

Toy Story 4上映後廣受好評!它的評分為8.7,國内票房為3.4億美元,國際票房為2.7億美元。将記錄添加到BoxOffice表中。

跟新表資料

在sqlbolt上學習SQL

1.The director for A Bug's Life is incorrect, it was actually directed by John Lasseter

A Bug's Life的導演是錯誤的,實際上是John Lasseter導演的

2.The year that Toy Story 2 was released is incorrect, it was actually released in 1999

Toy Story 2釋出的年份是不正确的,它實際上是在1999年上映的

3.Both the title and director for Toy Story 8 is incorrect! The title should be "Toy Story 3" and it was directed by Lee Unkrich

Toy Story 2的片名和導演都是錯誤的!标題應該是“Toy Story 3”,它是由李昂克裡奇導演的

帶條件的删除語句

在sqlbolt上學習SQL

1.This database is getting too big, lets remove all movies that were released before 2005.

這個資料庫太大了,讓我們删除所有2005年之前上映的電影。

2.Andrew Stanton has also left the studio, so please remove all movies directed by him.

Andrew Stanton也離開了工作室,是以請删除他執導的所有電影。

建立帶有可選表格限制和預設值的表格語句

Data type

<code>INTEGER</code>, <code>BOOLEAN</code>

整數資料類型可以存儲整數值,如數字的計數或年齡。在某些實作中,布爾值隻是表示為0或1的整數值。

<code>FLOAT</code>, <code>DOUBLE</code>, <code>REAL</code>

浮點資料類型可以存儲更精确的數值資料,如測量值或小數值。根據該值所需的浮點精度,可以使用不同的類型。

<code>CHARACTER(num_chars)</code>, <code>VARCHAR(num_chars)</code>, <code>TEXT</code>

基于文本的資料類型可以在各種地區存儲字元串和文本。在處理這些列時,不同類型之間的差別通常相當于底層資料庫的效率。CHARACTER和VARCHAR(可變字元)類型都是用它們可以存儲的最大字元數指定的(較長的值可能會被截斷),是以使用大表存儲和查詢更有效。

<code>DATE</code>, <code>DATETIME</code>

SQL還可以存儲日期和時間戳,以跟蹤時間序列和事件資料。使用它們可能很棘手,特别是在跨時區操作資料時。

<code>BLOB</code>

最後,SQL可以将二進制資料以blob的形式直接存儲在資料庫中。這些值對資料庫通常是不透明的,是以通常必須使用正确的中繼資料存儲它們,以便查詢它們。

Docs: MySQL, Postgres, SQLite, Microsoft SQL Server

Constraint

<code>PRIMARY KEY</code>

這意味着這個列中的值是唯一的,每個值都可以用來辨別該表中的一行。

<code>AUTOINCREMENT</code>

對于整型值,這意味着值将自動填充,并在每次插入行時遞增。不是所有資料庫都支援。

<code>UNIQUE</code>

這意味着這一列中的值必須是唯一的,是以不能在這一列中插入與表中另一行值相同的行。與“主鍵”不同的是,它不必是表中某一行的鍵。

<code>NOT NULL</code>

這意味着插入的值不能是' NULL '。

<code>CHECK (expression)</code>

這允許您運作更複雜的表達式來測試插入的值是否有效。例如,您可以檢查值是否為正,或大于特定的大小,或以特定的字首開頭,等等。

<code>FOREIGN KEY</code>

這是一種一緻性檢查,確定此列中的每個值與另一個表中某列中的另一個值相對應。例如,如果有兩個表,一個按ID列出所有雇員,另一個列出他們的工資資訊,那麼“FOREIGN KEY”可以確定工資表中的每一行都對應于主employee清單中的一個有效雇員。

1.建立一個名為Database的新表,包含以下列:

-Name描述資料庫名稱的字元串(文本)

-Version該資料庫的最新版本号(浮點數)

-Download_count下載下傳資料庫的整數計數

該表沒有限制。

修改表以添加新列

修改表以删除列

更改表名

在sqlbolt上學習SQL

1.Add a column named **Aspect_ratio** with a **FLOAT** data type to store the aspectratio each movie was released in.

添加一個名為 Aspect _ ratio 的列,該列具有 FLOAT 資料類型,用于存儲年釋出的每部電影的 Aspect-ratio。

2.Add another column named **Language** with a **TEXT** data type to store the language that the movie was released in. Ensure that the default for this language is **English**.

添加另一個名為 Language 的帶有 TEXT 資料類型的列,以存儲電影發行的語言。確定此語言的預設值為英語。

Drop table statement

在sqlbolt上學習SQL

1. We've sadly reached the end of our lessons, lets clean up by removing the *Movies* table

我們已經很遺憾地結束了我們的課程,讓我們删除Movies表

2. And drop the *BoxOffice* table as well

并且同樣的去除BoxOffice表