日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

数据库 CURD测试题【简单】

發布時間:2023/12/10 数据库 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据库 CURD测试题【简单】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1.組合兩個表
      • 基本信息
      • 要求
      • 答案
    • 2.第二高的薪水
      • 基本信息
      • 要求
      • 答案
    • 3.查找重復的電子郵箱
      • 基本信息
      • 要求
      • 答案
    • 4.超過經理收入的員工
      • 基本信息
      • 要求
      • 答案:
    • 5.超過5名學生的課
      • 信息:
      • 要求
      • 答案
    • 6.有趣的電影
      • 信息
      • 要求
      • 答案
    • 7.交換工資(updeta,條件判斷)
      • 基本信息
      • 要求
      • 答案
    • 8從不訂購的客戶
      • 信息
      • 要求
      • 答案
        • 使用的not in
        • 使用的not exists
    • 9.上升的溫度(mysql函數to_days)
        • 信息
        • 要求
        • 答案
    • 10.刪除重復的郵箱
        • 信息
        • 要求
        • 答案
    • 11.員工獎金(null的應用)
        • 信息
        • 要求
    • 12.查找訂單最多的用戶

1.組合兩個表

基本信息

表1: Person

列名類型
PersonIdint
FirstNamevarchar
LastNamevarchar
  • PersonId 是上表主鍵
    表2: Address
列名類型
AddressIdint
PersonIdint
Cityvarchar
Statevarchar
  • AddressId 是上表主鍵

要求

編寫一個 SQL 查詢,滿足條件:無論 person 是否有地址信息,都需要基于上述兩表提供 person 的以下信息:

FirstName, LastName, City, State

答案

select Person.FirstName,Person.LastName,Address.City,Address.State from Person left join Address on Person.PersonId = Address.PersonId;

2.第二高的薪水

基本信息

編寫一個 SQL 查詢,獲取 Employee 表中第二高的薪水(Salary) 。

IdSalary
1100
2200
3300

要求

例如上述 Employee 表,SQL查詢應該返回 200 作為第二高的薪水。如果不存在第二高的薪水,那么查詢應返回 null。

SecondHighestSalary
200

答案

select (select DISTINCT Salary from Employee ORDER BY Salary DESC limit 1 offset 1 ) as SecondHighestSalary;

3.查找重復的電子郵箱

基本信息

編寫一個 SQL 查詢,查找 Person 表中所有重復的電子郵箱。

示例:

IdEmail
1a@b.com
2c@d.com
3a@b.com

要求

根據以上輸入,你的查詢應返回以下結果:

Email
a@b.com

說明:所有電子郵箱都是小寫字母。

答案

select Email from Person group by Email having count(Email)>1;

4.超過經理收入的員工

基本信息

Employee 表包含所有員工,他們的經理也屬于員工。每個員工都有一個 Id,此外還有一列對應員工的經理的 Id。

IdNameSalaryManagerId
1Joe700003
2Henry800004
3Sam60000NULL
4Max90000NULL

要求

給定 Employee 表,編寫一個 SQL 查詢,該查詢可以獲取收入超過他們經理的員工的姓名。在上面的表格中,Joe 是唯一一個收入超過他的經理的員工。

Employee
Joe

答案:

Select e1.Name as Employee from Employee e1 join Employee e2 on e1.ManagerId = e2.Id and e1.Salary > e2.Salary

5.超過5名學生的課

信息:

有一個courses 表 ,有: student (學生) 和 class (課程)。

請列出所有超過或等于5名學生的課。

例如,表:

studentclass
AMath
BEnglish
CMath
DBiology
EMath
FComputer
GMath
HMath
IMath

要求

應該輸出:

class
Math
  • Note:
    學生在每個課中不應被重復計算。

答案

select class from courses group by class having count(DISTINCT student) >= 5;

6.有趣的電影

信息

某城市開了一家新的電影院,吸引了很多人過來看電影。該電影院特別注意用戶體驗,專門有個 LED顯示板做電影推薦,上面公布著影評和相關電影描述。

作為該電影院的信息部主管,您需要編寫一個 SQL查詢,找出所有影片描述為非 boring (不無聊) 的并且 id 為奇數 的影片,結果請按等級 rating 排列。

例如,下表 cinema:

idmoviedescriptionrating
1Wargreat 3D8.9
2Sciencefiction8.5
3irishboring6.2
4Ice songFantacy8.6
5House cardInteresting9.1

要求

對于上面的例子,則正確的輸出是為:

idmoviedescriptionrating
5House cardInteresting9.1
1Wargreat 3D8.9

答案

select id,movie, description, rating from cinema where description !='boring' and id %2=1 order by rating desc;

7.交換工資(updeta,條件判斷)

基本信息

給定一個 salary 表,如下所示,有 m = 男性 和 f = 女性 的值。交換所有的 f 和 m 值(例如,將所有 f 值更改為 m,反之亦然)。要求只使用一個更新(Update)語句,并且沒有中間的臨時表。

注意,您必只能寫一個 Update 語句,請不要編寫任何 Select 語句。

例如:

idnamesexsalary
1Am2500
2Bf1500
3Cm5500
4Df500

要求

運行你所編寫的更新語句之后,將會得到以下表:

idnamesexsalary
1Af2500
2Bm1500
3Cf5500
4Dm500

答案

UPDATE salary SET sex=IF(sex='f','m','f');

8從不訂購的客戶

信息

某網站包含兩個表,Customers 表和 Orders 表。編寫一個 SQL 查詢,找出所有從不訂購任何東西的客戶。

Customers 表:

IdName
1Joe
2Henry
3Sam
4Max

Orders 表:

IdCustomerId
13
21

要求

例如給定上述表格,你的查詢應返回:

Customers
Henry
Max

答案

使用的not in

select Name as Customers from Customers where Customers.Id not in (select Customers.Id from Orders where Customers.Id = Orders.CustomerId);

使用的not exists

select Name as Customers from Customers where not exists (select * from Orders where Customers.Id = Orders.CustomerId);

9.上升的溫度(mysql函數to_days)

信息

給定一個 Weather 表,編寫一個 SQL 查詢,來查找與之前(昨天的)日期相比溫度更高的所有日期的 Id。

Id(INT)RecordDate(DATE)Temperature(INT)
12015-01-0110
22015-01-0225
32015-01-0320
42015-01-0430

例如,根據上述給定的 Weather 表格,返回如下 Id:

要求

Id
2
4

答案

select w1.Id from weather as w1,weather as w2 where w1.Temperature > w2.Temperature and to_days(w1.RecordDate)-to_days(w2.RecordDate)=1;

10.刪除重復的郵箱

信息

編寫一個 SQL 查詢,來刪除 Person 表中所有重復的電子郵箱,重復的郵箱里只保留 Id 最小 的那個。

IdEmail
1john@example.com
2bob@example.com
3john@example.com
  • Id 是這個表的主鍵。

要求

例如,在運行你的查詢語句之后,上面的 Person 表應返回以下幾行:

IdEmail
1john@example.com
2bob@example.com

答案

delete p1 from Person p1, Person p2 where p1.Id>p2.Id and p1.Email = p2.Email;

11.員工獎金(null的應用)

信息

select all employee’s name and bonus whose bonus is < 1000.

Table:Employee

empIdnamesupervisorsalary
1John31000
2Dan32000
3Bradnull4000
4Thomas34000

empId is the primary key column for this table.
Table: Bonus

empIdbonus
2500
42000
  • empId is the primary key column for this table.

要求

Example ouput:

namebonus
Johnnull
Dan500
Bradnull

選出所有獎金<1000元的雇員姓名及獎金數額

答案:

select name, bonus from Employee e left join Bonus b on e.empId = b.empId where bonus < 1000 or bonus is null

12.查找訂單最多的用戶

總結

以上是生活随笔為你收集整理的数据库 CURD测试题【简单】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。