Niuke SQL は、50 セットの質問コードと分析を知っている必要があります。

演習リンク:
https://www.nowcoder.com/exam/oj?page=1&tab=SQL%E7%AF%87&topicId=298

1. データを取得する

SQL60 は Customers テーブルからすべての ID を取得します

select *
from Customers

SQL61 注文された製品の取得と一覧表示

select distinct prod_id
from OrderItems

SQL62 はすべての列を取得します

select *
from Customers

2. データの並べ替えと取得

SQL63 顧客名の取得と並べ替え

select cust_name
from Customers
order by cust_name desc

SQL64 は顧客 ID と日付を並べ替えます

select cust_id,order_num
from Orders
order by cust_id,order_date desc

数量と価格によるSQL65ソート

select quantity, item_price
from OrderItems
order by quantity desc,item_price desc

SQL66 チェック SQL ステートメント

SELECT vend_name
FROM Vendors 
ORDER by vend_name DESC;

3. データのフィルタリング

SQL67 は固定価格の製品を返します

select prod_id,prod_name
from Products
where prod_price=9.49

SQL68 はより高い価格の製品を返します

select prod_id,prod_name
from Products
where prod_price >=9

SQL69 は価格でソートされた製品を返します

select prod_name,prod_price
from Products
where prod_price between 3 and 6
order by prod_price

SQL70 はより多くの製品を返します

select order_num
from OrderItems
where quantity>=100
group by order_num

4.高度なデータフィルタリング

SQL71 ベンダー名を取得する

select vend_name
from Vendors
where vend_country='USA' and vend_state='CA'

SQL72 は注文された製品を取得して一覧表示します

select order_num,prod_id,quantity
from OrderItems
where prod_id in ('BR01','BR02','BR03') and quantity>100

SQL73 は、価格が $3 ~ $6 のすべての製品の名前と価格を返します。

select prod_name, prod_price
from Products
where prod_price>=3 and prod_price<=6
order by prod_price

SQL74 エラー修正

SELECT vend_name 
FROM Vendors 
WHERE vend_country = 'USA' AND vend_state = 'CA'
ORDER BY vend_name 

5. ワイルドカードでフィルタリングする

SQL75 は製品名と説明を取得します (1)

select prod_name,prod_desc
from Products
where prod_desc like '%toy%'

SQL76 は製品名と説明を取得します (2)

select prod_name,prod_desc
from Products
where prod_desc not like '%toy%'
order by prod_name

SQL77 は製品名と説明を取得します (3)

select prod_name,prod_desc
from Products
where prod_desc like '%toy%' and prod_desc like '%carrots%'

SQL78 は製品名と説明を取得します (4)

# 方法一
select prod_name,prod_desc
from Products
where prod_desc like '%toy%carrots%' 
# 方法二
select prod_name,prod_desc
from Products
where prod_desc regexp '.*toy.*carrots.*' 

6. 計算フィールドを作成する

SQL79 エイリアス

select vend_id,
vend_name as vname,
vend_address as vaddress,
vend_city as vcity
from Vendors
order by vname

SQL80割引

select prod_id,
prod_price,
prod_price*0.9 as sale_price
from Products

7. 関数を使用してデータを処理する

SQL81 顧客ログイン名( 1.concat はスプライシングに使用され、2.upper は大文字変換に使用されます)

select cust_id, cust_name,
upper(concat(substring(cust_contact,1,2),substring(cust_city,1,3))) as user_login
from Customers

SQL82 は 2020 年 1 月のすべての注文番号と注文日を返します

select order_num,order_date
from Orders
where year(order_date)='2020' and month(order_date)='01'
order by order_date

8. データを要約する

SQL83 販売された製品の総数を決定する

select sum(quantity) as items_ordered
from OrderItems

SQL84 販売された製品アイテムの総数を決定する BR01

select sum(quantity) as items_ordered
from OrderItems
where prod_id= 'BR01'

SQL85 Products テーブルで、価格が 10 ドルを超えない最も高価な製品の価格を決定する

select max(prod_price) as max_price
from Products
where prod_price <=10

9. パケットデータ

SQL86 は各注文番号の行数を返します

select order_num,
count(order_num) as order_lines
from OrderItems
group by order_num
order by order_lines

SQL87 ベンダー製品あたりの最低コスト

select vend_id,
min(prod_price) as cheapest_item
from Products
group by vend_id
order by cheapest_item

SQL88 最良の顧客の決定

select order_num
from OrderItems
where quantity >=100
group by order_num
order by order_num

SQL89 最適な顧客を決定する別の方法 (1)
( 1. group by、where、having、order by の使用順序: where、group by、having、order by; 2. where はレコードの制限、およびhaving はグループ化後の結果を制限するため、where は group by の前に、having は group by の後に 3. where の後の条件式では集計関数を使用できませんが、having は使用できるため、集計関数に遭遇した場合は having を使用します

select order_num,
sum(quantity * item_price) as total_price
from OrderItems
group by order_num
having total_price >=1000
order by order_num

SQL90 エラー訂正 (3)

SELECT order_num, COUNT(*) AS items 
FROM OrderItems 
GROUP BY order_num 
HAVING COUNT(*) >= 3 
ORDER BY items, order_num;

10. サブクエリを使用する

SQL91 は、10 ドル以上の価格の製品を購入した顧客のリストを返します。

select cust_id
from Orders
where order_num in (
        select order_num
        from OrderItems
        where item_price >=10
)

SQL92 製品 ID が BR01 である製品を購入した注文を特定する (1)

select cust_id,order_date
from Orders
where order_num in (
    select order_num
    from OrderItems
    where prod_id='BR01'
)
order by order_date

SQL93 は、製品 ID が BR01 である製品を購入したすべての顧客の電子メールを返します (1)

select cust_email
from Customers
where cust_id in (
    select cust_id
    from Orders
    where order_num in (
        select order_num
        from OrderItems
        where prod_id='BR01'
    )
)

SQL94 は、顧客ごとに異なる注文の合計金額を返します

select cust_id,
(select sum(quantity*item_price)
from OrderItems 
where OrderItems.order_num=Orders.order_num) as total_ordered
from Orders 
order by total_ordered desc

SQL95 Products テーブルのすべての製品名と対応する売上合計を取得する

select prod_name,
(select sum(quantity)
from OrderItems
where OrderItems.prod_id=Products.prod_id) as quant_sold
from Products

11.ジャンクションテーブル

SQL96 は顧客名と関連する注文番号を返します

#方法一
select cust_name,order_num
from Customers
join Orders using(cust_id)
order by cust_name,order_num

#方法二
select cust_name,order_num
from Customers as a,Orders as b
where a.cust_id=b.cust_id
order by cust_name,order_num

#方法三
select cust_name,order_num
from Customers as a
inner join Orders as b
on a.cust_id=b.cust_id
order by cust_name,order_num

SQL97 は、顧客名と関連する注文番号、および各注文の合計金額を返します。

select cust_name,order_num,
sum(quantity*item_price) as OrderTotal
from Customers
join Orders using(cust_id)
join OrderItems using(order_num)
group by cust_name,order_num
order by cust_name,order_num

SQL98 製品 ID が BR01 である製品を購入した注文を特定する (2)

select cust_id,order_date
from OrderItems
join Orders using(order_num)
where prod_id='BR01'
order by order_date

SQL99 は、製品 ID が BR01 である製品を購入したすべての顧客の電子メールを返します (2)

select cust_email
from Customers
inner join Orders using(cust_id)
inner join OrderItems using(order_num)
where prod_id='BR01'
group by cust_email 

SQL100 最良の顧客を決定する別の方法 (2)

select cust_name,
sum(item_price * quantity) as total_price
from Customers
inner join Orders using(cust_id)
inner join OrderItems using(order_num)
group by  cust_name
having total_price >=1000
order by total_price

12.高度なリンクを作成する

SQL101 各顧客の名前とすべての注文番号を取得する (1)

select cust_name,order_num
from Customers
inner join Orders using(cust_id)
order by cust_name

SQL102 各顧客の名前とすべての注文番号を取得する (2)

select cust_name,order_num
from Customers
left join Orders using(cust_id)
order by cust_name

SQL103 は、製品名とそれに関連付けられた注文番号を返します

select prod_name,order_num
from Products
left join OrderItems using(prod_id)
order by prod_name

SQL104 は、製品名と各製品の合計注文数を返します

select prod_name,
count(order_num) as orders
from Products
left join OrderItems using(prod_id)
group by prod_name
order by prod_name

SQL105 リストのサプライヤーとそのサプライヤーが提供できる製品の数量

select vend_id,
count(prod_id) as prod_id
from Products
right join Vendors using(vend_id)
group by vend_id
order by vend_id

13.複合クエリ

SQL106 2 つの select ステートメントを組み合わせる (1)

select prod_id,quantity
from OrderItems
where quantity=100
union all
select prod_id,quantity
from OrderItems
where prod_id like'BNBG%'
order by prod_id

SQL107 2 つの select ステートメントを組み合わせる (2)

select prod_id,quantity
from OrderItems
where quantity=100 or prod_id like'BNBG%'
order by prod_id

SQL108 Products テーブルの製品名と Customers テーブルの顧客名を組み合わせる

select  prod_name
from Products
union all
select cust_name as prod_name
from Customers
order by prod_name

SQL109 エラー訂正 (4)
(複合クエリでは、並べ替えはすべてのステートメントの最後にのみ配置でき、一度だけ表示されます)

SELECT cust_name, cust_contact, cust_email 
FROM Customers 
WHERE cust_state = 'MI' 
UNION 
SELECT cust_name, cust_contact, cust_email 
FROM Customers 
WHERE cust_state = 'IL'
ORDER BY cust_name;

おすすめ

転載: blog.csdn.net/qq118640594X/article/details/126914912