0
0

SQLServerの勉強用問題(11~20)

Posted at

新入社員の練習課題にSQL問題集を探していたところ、英語ですが100問を公開しているサイトがありましたのでご紹介。

AdventureWorks: SQL Exercises, Practice, Solution
https://www.w3resource.com/sql-exercises/adventureworks/adventureworks-exercises.php

前提としてSQLServerとサンプルデータAdverntureWorksが必要です。

AdventureWorks サンプル データベース
https://learn.microsoft.com/ja-jp/sql/samples/adventureworks-install-configure?view=sql-server-ver16&tabs=ssms

ぎりぎり理解できそうな和訳(11~20):

11. 次のテーブルから、姓が文字 'L' で始まる人物を検索する SQL クエリを作成しなさい。
BusinessEntityID、FirstName、LastName、および PhoneNumber を返します。結果を lastname と firstname でソートします。
テーブル:Person.Person
テーブル:Person.PersonPhone

12. 次のテーブルから、小計の合計を求める SQL クエリを作成しなさい。
salespersonidとcustomeridでグルーピングします。
salespersonid、customerid、sum_subtotal(小計の合計)を返します。
テーブル:sales.salesorderheader

13. 次のテーブルから、locationid と self の組み合わせごとの quantity の合計と、全体の quantity の合計を求めるSQLクエリを作成しなさい。
locationid、shelf、TotalQuantity(数量の合計) を返します。
テーブル:production.productinventory

補足:
locationidとselfのすべての組み合わせでの小計と総計(cube)
を出せとのことです。

14. 次のテーブルから、locationid と self の組み合わせとグループごとの quantity の合計と、全体の quantity の合計を求めるSQLクエリを作成しなさい。
locationid、shelf、TotalQuantity(数量の合計) を返します。
テーブル:production.productinventory

補足:
locationidとselfのグループごとの小計と総計(rollup)、
locationidとselfのすべての組み合わせでの小計と総計(cube)
を出せとのことです。

15. 次のテーブルから、locationid ごとの quantity の合計と、全体の quantity の合計を求めるSQLクエリを作成しなさい。
locationid、TotalQuantity(数量の合計) を返します。
テーブル:production.productinventory

補足:
GROUPING SETS を使えとのことです。

16. 次のテーブルから、各都市に住んでいる従業員数を取得する SQL クエリを作成しなさい。
都市と従業員数を返します。結果を city の昇順でソートします。
テーブル:Person.BusinessEntityAddress

17. 次のテーブルから、各年の売上合計を取得する SQL クエリを作成しなさい。
注文日の年と支払金額の合計を返しなさい。結果を注文日の日付の昇順に並べ替えなさい。
テーブル:Sales.SalesOrderHeader

18. 次のテーブルから、各年の売上合計を取得する SQL クエリを作成します。
注文年が 2016 年以前の注文についてのみ出力してください。
注文日の年と合計金額を返します。注文日の年の昇順でソートします。
テーブル:Sales.SalesOrderHeader

19. 次のテーブルから、部署のマネージャーの連絡先を検索する SQL クエリを作成しなさい。
ContactTypeID, name を返します。結果をNameの降順にソートしてください。
テーブル Person.ContactType

20. 次のテーブルから、'Purchasing Manager' の連絡先を検索する SQL クエリを作成しなさい。
BusinessEntityID、LastName、および FirstName を返します。結果を LastName および FirstName の昇順に並べ替えます。
テーブル:Person.BusinessEntityContact

参考:
ROLLUP、CUBE、GROUPING SETS などの新しい SQL 構文を使用して Amazon Redshift のオンライン分析処理 (OLAP) クエリを簡略化
https://aws.amazon.com/jp/blogs/news/simplify-online-analytical-processing-olap-queries-in-amazon-redshift-using-new-sql-constructs-such-as-rollup-cube-and-grouping-sets/

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0