第一章
What is SQL?
a database: a collection of data stored in a format that can easily be accessed.
Database Management System(DBMS):
1. Relational
把数据储存在利用关系来互相链接的表中(eg: 顾客、订单、产品),每个表储存一类特定对象的数据。SQL是我们用来处理这些关系型DBMS的语言,来修改这些data。
常用的RDMS: MySQL,微软的SQL server, Oracle
2. Non-Relational (NoSQL)
没有表和关系,也无法读取SQL语言
第二章
The Select Statement
不分大小写
“;”终止一段
-- 表示注释
USE sql_store;
SELECT *
FROM customers
-- WHERE customer_id=1
ORDER BY first_name
1. Select clause
*表示全选
添加alias
SELECT
first_name,
last_name,
points * 10 + 100 AS 'discount_factor'
删去重复项
SELECT DISTINCT state
FROM customers
练习
USE sql_store;
SELECT name, unit_price, (unit_price * 1.1) AS 'new_price'
FROM products
注意细节,“_”不能用空格替代
2. Where Clause
做filter
取积分高于3000的顾客
SELECT *
FROM Customer
WHERE points > 3000
运算符号
!= <> 都表示不等于
1900-01-01是日期标准的表述形式
3. and or not
and会被优先执行,可用()改变
4. IN 运算符
SELECT *
FROM Customers
WHERE state= 'VA' or state='GA' or state='FL'
注意:OR combine conditions; SQL cannot combine a string with a Boolean expression that produces a Boolean Value which can be True or False
IN
SELECT *
FROM Customers
WHERE state IN ( 'VA' , 'GA' ,'FL' )
5. Between 运算符
WHERE points BETWEEN 1000 AND 3000
less than or equal
not limited to using num, e.g: BETWEEN '1990-01-01' AND '2019-01-01'
6. LIKE运算符
retrieve row that match a specific pattern
以B开头的Last name
WHERE last_name LIKE 'b%'
# %表示任意字符数,可以放任意位置
# _ 表示一个字符
#'_y'表示last name is exactly 2 characters' long
练习
SELECT *
FROM customers
WHERE address LIKE '%TRAIL%' or
address LIKE '%AVENUE%'
or不能直接跟LIKE '..' 要写上address
可以用NOT LIKE来排除
7. REGEXP运算符
regular expression
WHERE last_name REGEXP 'field'
# contains 'field'
WHERE last_name REGEXP '^field'
# ^ 最开头的是field
WHERE last_name REGEXP 'field$'
# $ 最末尾的是field
WHERE last_name REGEXP 'field|Mac'
# | 'logical or' multiple search patterns
WHERE last_name REGEXP '[gim]e'
# [] ge ie me
WHERE last_name REGEXP '[a-h]e'
# abcdefgh + e
不要打多余的空格
8. IS NULL
SELECT *
FROM customers
WHERE phone IS NULL
或者
WHERE phone IS NOT NULL
9. ORDER BY
SELECT *
FROM customers
ORDER BY first_name DESC
# DESC 降序排列
ORDER BY state, first_name
# 先按照state,再按first_name
不管这个column是不是在select里面的,都可以作为排序的规则,如:
SELECT first_name, last_name
FROM customers
OREDER BY birth_date
练习:
SELECT *, quantity * unit_price AS total_price
FROM order_items
WHERE order_id=2
ORDER BY quantity * unit_price DESC
10.limit 子句
前几
SELECT *
FROM customers
LIMIT 3 #提取前3个
LIMIT 6, 3 #跳过前6个,提取后面3个
第三章
1. Inner Joins 在多张表格中检索数据
SELECT *
FROM orders
JOIN customs
ON orders.customer_id = customers.customers_id
#把两张表连在一起 也可以写INNER JOIN
#当不同的表有相同的column时,select 要加一个前缀,order.customer_id
利用alias来避免重名
SELECT order_id, o.customers_id, first_name, last_name
FROM orders o # 在表紧随其后 可以加一个字母 表示alias
JOIN customs c
ON o.customer_id = c.customers_id
2. Joining Across Database
SELECT *
FROM order_items oi
JOIN sql_inventory.products p
ON oi.product_id = p.product_id
如果是不同的table,加一个前缀就好
3. Self Joins
USE sql_hr;
SELECT
e.employee_id,
e.first_name,
m.first_name AS manager
FROM employees e
JOIN employees m
ON e.reports_to= m.employee_id
4.Joining Multiple Tables
USE sql_store;
SELECT
o.order_id,
o.order_date,
c.first_name,
c.last_name,
os.name AS status
FROM orders o
JOIN customers c
ON o.customer_id= c.customer_is
JOIN order_statuses os
ON o.status=os.order_status_id
5.Implicit Join Syntax
6.Outer Joins
SELECT
c.customer_id,
c.first_name,
o.order_id
FROM customers c
JOIN orders o
ON c.customer_id= o.customer_id
only returning the tables that match the joint condition.如果有订单,就会被返回。但是如果顾客没有订单呢?如何让他也返回?
左连接+右连接
SELECT
c.customer_id,
c.first_name,
o.order_id
FROM customers c
LEFT JOIN orders o
##左边的表格(customers)不管满不满足JOIN的条件,都会被返回
ON c.customer_id= o.customer_id
7.Outer Join Between Multiple tables
避免右链接
8. Self Outer Joins
参考3.self joins
问题:这样返回的是只有manager的表格,而那些没有manager的人(比如CEO)要如何join?
——用Left Join,可以得到所有的员工,不管他们有没有对应的管理人员
9.The Using Clause
Join两个表格On某一种条件,这样的逻辑随着查询功能的复杂而变得难以理解。而Using子句可以简化查询。
--JOIN customesr c
--ON o.customer_id=c.customer_id
#有相同的customer_id,
#用using子句来简化
USING (customer_id)
LEFT JOIN shippers sh
#有一些订单是没有发货的
#用外连接显示所有的订单
USING (shipper_id)
注意:USING只能在不同表格的列目名称完全相同的情况下才能使用
如何用USING连接多个列?
SELECT *
FROM 表1
JOIN 表2
USING (order_id, product_id)
10. Natural Joins
连接两个表格的语句,但是容易出错。数据会自动连接共同的列
SELECT *
FROM ordres o
NATURAL JOIN customers c
11.Cross Joins
连接第一个表的每条记录和第二个表的记录
SELECT *
FROM customers c
CROSS JOIN products p #不用写条件
ORDER BY c.first_name
implicit syntax
FROM customer c, products p
12.Unions
合并多段查询记录
SELECT
order_id,
order_date,
‘Active’ AS status
FROM orders
WHERE order_date >= '2019-01-01'
UNION
SELECT
order_id,
order_date,
‘Archived’ AS status
FROM orders
WHERE order_date < '2019-01-01'
合并不同表格的记录
SELECT first_name
FROM customers
UNION
SELECT name
FROM shippers
注意:返回的列数必须相同