1. 程式人生 > 其它 >資料庫系統之SQL SELECT語句-2

資料庫系統之SQL SELECT語句-2

技術標籤:# SQL資料庫系統資料庫SQLSELECT

SQL - SELECT Statement -2

Set Algebra Queries

包含三種操作:
UNION, INTERSECTION 和DIFFERENCE。

用圖可以更直接的表達三者的關係:
在這裡插入圖片描述
A UNION B:在A或在B中的所有元素
A INTERSECTION B: 同時在A和B中的所有元素

A DIFFERENCE B: 所有在A中,而不在B中的元素。

UNION Operation

查詢所有由物理系或數學系提供的學分,並且不要列出重複的行:
有兩種寫法,第二種就是使用UNION的寫法。

SELECT DISTINCT credits
FROM COURSE
WHERE (offered_by = 'Physics') OR (offered_by = 'Mathematics');


SELECT credits
FROM COURSE
WHERE offered_by = 'Physics'
UNION
SELECT credits
FROM COURSE
WHERE offered_by =
'Mathematics';

要注意的是UNION操作會自動消除重複的行,同時 A UNION B = B UNION A。

如果不想要UNION操作會自動消除重複的行,那麼需要使用UNION ALL operation

SELECT credits
FROM COURSE
WHERE offered_by = 'Physics'
UNION ALL
SELECT credits
FROM COURSE
WHERE offered_by = 'Mathematics';

第一種寫法的話去掉DISTINCT 語句即可。

INTERSECT Operation

查詢所有由物理系和數學系共同管理的課程學分:

SELECT credits
FROM COURSE
WHERE offered_by = 'Physics'
INTERSECT
SELECT credits
FROM COURSE
WHERE offered_by = 'Mathematics';

INTERSECT操作也會自動消除重複的行,同時 A INTERSECT B = B INTERSECT A。

MINUS Operation

查詢由物理系而不是由數學系負責的所有課程的學分:

SELECT credits
FROM COURSE
WHERE offered_by = 'Physics'
MINUS
SELECT credits
FROM COURSE
WHERE offered_by = 'Mathematics';

INTERSECT操作會自動消除左邊引數的重複行,但是 A MINUS B ≠ B MINUS A。
要分清左右,這裡的左就是Physics。

但是有的時候你的SQL不支援INTERSECT和 MINUS語句,那麼你可以轉換一下:
比如轉換INTERSECT

SELECT DISTINCT credits
FROM COURSE
WHERE offered_by = 'Physics' AND
			credits IN (SELECT credits
			FROM COURSE
			WHERE offered_by = 'Mathematics');

Sorting

如果查詢,排序將重新對結果中的行進行排序。

查詢所有6個學分課程的編號和名稱,按名稱升序排列:

SELECT cnum, title
FROM COURSE
WHERE credits = 6
ORDER BY title ASC;

SELECT cnum, title
FROM COURSE
WHERE credits = 6
ORDER BY 2 ASC;

ASC代表升序,DESC代表降序。

ORDER BY 2 ASC這裡的2代表SELECT裡面的第二個title。

按學分升序查詢所有課程的編號、名稱和學分,按名稱降序查詢相同學分的所有課程:

SELECT cnum, title, credits
FROM COURSE
ORDER BY credits ASC, title DESC;

SELECT cnum, title, credits
FROM COURSE
ORDER BY 3 ASC, 2 DESC;

缺失值查詢

SELECT title
FROM COURSE
WHERE offered_by IS NULL;

SELECT title
FROM COURSE
WHERE offered_by = NULL;

Grouping

Grouping在應用組函式之前對行進行分組.
.
GROUP BY根據GROUP中包含的屬性對關係表進行排序,並在GROUP BY list中建立具有相同屬性值的行組.

舉例:

對於每個學分值,找到擁有相應學分的課程總數:

SELECT credits, count(*)
FROM COURSE
GROUP BY credits;

找到每個部門提供的學分總數:

SELECT offered_by, sum(credits)
FROM COURSE
GROUP BY offered_by;

Grouping With Selections

找出提供一門以上課程的所有系的名稱:

SELECT offered_by, count(*)
FROM COURSE
GROUP BY offered_by;

從下表可以看出offered_by這個屬性裡的行被按照首字母順序升序排列了。後面緊跟著是count的數量。

offered_bycount(*)
Computer Science2
Mathematics1
Physics2
SELECT offered_by, count(*)
FROM COURSE
GROUP BY offered_by
HAVING count(*) > 1;

使用HAVING語句可以使用條件對SELECT出的結果進行再次篩選:

offered_bycount(*)
Computer Science2
Physics2

References

  1. T. Connoly, C. Begg, Database Systems, A Practical Approach to Design, Implementation, and Management, Chapters 6.3.1 - 6.3.4 Data Manipulation, Pearson Education Ltd, 2015.
  2. D. Darmawikarta, Oracle SQL : A Beginner’s Tutorial Brainy Software Inc. First Edition: June 2014.