SQL复建Day01

以前学的SQL语句都忘了,借力扣再熟悉下。
基本语句格式:

select
	属性1,属性2...,属性N
from
	SQLwhere
	条件语句1
	and / or
	条件语句2
	...
;

一些小tricks:

属性重命名 - as
	select name as customer from ...;
	
表重命名:
	(select * from 表x)新名字
	
数字、字符串的比较:
	><=!=<> (i.e. 不包括)
	
NULL的处理 - is
	属性x is/is not NULL

一些例题:
SQL复建Day01_第1张图片

1select
		name, population, area
	from 
		World
	where
		area >= 3000000
		or population >= 25000000
	;2select 
		name, population, area
	from
		World
	where
		area >= 3000000
	union
	select
		name, population, area
	where
		population >= 25000000
	;

SQL复建Day01_第2张图片

	select
		product_id
	from 
		Products
	where
		low_fats = "Y"
		and recyclable = "Y"
	;

SQL复建Day01_第3张图片

1select 
		name
	from 
		customer
	where 
		referee_id != 2
		or referee_id is NULL
	;2select 
		name
	from 
		customer
	where
		referee_id <> 2
		or referee_id is NULL
	;

SQL复建Day01_第4张图片

1select
		name as Customers
	from
		Customers
	where
		id not in (
			select
				customerId
			from
				Orders
		) order_customer
	;2select		name as Customers
	from
		( select
				Name, CustomerId
			from
					Customers
				left join
					Orders
				on
					Customers.id = Orders.id
		) join_order
	where
		customerId is NULL
	;

你可能感兴趣的:(sql,sql,数据库,mysql)