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

歡迎訪問 生活随笔!

生活随笔

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

数据库

SQL基础练习

發布時間:2024/4/11 数据库 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQL基础练习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

花了一天時間看了一下尚硅谷的sql視頻,自己做了一下老師提供的練習題并進行核對,算是把sql基礎復習了一遍。資料放在文末可自取。

表結構

employees員工表

一、 基本的語句

題目

  • 下面的語句是否可以執行成功
  • select last_name , job_id , salary as sal from employees;
  • 下面的語句是否可以執行成功
  • select * from employees;
  • 找出下面語句中的錯誤
  • select employee_id , last_name, salary * 12 “ANNUAL SALARY” from employees;
  • 顯示表 departments 的結構,并查詢其中的全部數據
  • 顯示出表 employees 中的全部 job_id(不能重復)
  • 顯示出表 employees 的全部列,各個列之間用逗號連接,列頭顯示成 OUT_PUT
  • 練習

    #1. 下面的語句是否可以執行成功 SELECT last_name , job_id , salary AS sal FROM employees; #2.下面的語句是否可以執行成功 SELECT * FROM employees; #3.找出下面語句中的錯誤 SELECT employee_id , last_name, salary * 12 AS "ANNUAL SALARY" FROM employees;#4.顯示表departments的結構,并查詢其中的全部數據 SELECT * FROM `departments`;#5.顯示出表employees中的全部job_id(不能重復) SELECT DISTINCT job_id FROM employees;#6.顯示出表employees的全部列,各個列之間用逗號連接,列頭顯示成OUT_PUT SELECT IFNULL(commission_pct,0) AS 獎金率,commission_pct FROM employees; #------------------------------------------- SELECTCONCAT(`first_name`,',',`last_name`,',',`job_id`,',',IFNULL(commission_pct,0)) AS out_put FROMemployees;

    二、條件查詢(P267)

    題目

  • 查詢工資大于 12000 的員工姓名和工資
  • 查詢員工號為 176 的員工的姓名和部門號和年薪
  • 選擇工資不在 5000 到 12000 的員工的姓名和工資
  • 選擇在 20 或 50 號部門工作的員工姓名和部門號
  • 選擇公司中沒有管理者的員工姓名及 job_id
  • 選擇公司中有獎金的員工姓名,工資和獎金級別
  • 選擇員工姓名的第三個字母是 a 的員工姓名
  • 選擇姓名中有字母 a 和 e 的員工姓名
  • 顯示出表 employees 表中 first_name 以 'e’結尾的員工信息
  • 顯示出表 employees 部門編號在 80-100 之間 的姓名、職位
  • 顯示出表 employees 的 manager_id 是 100,101,110 的員工姓名、職位
  • 練習

    # 1. 查詢工資大于 12000 的員工姓名和工資 SELECT last_name,salary from employees where salary>12000# 2. 查詢員工號為 176 的員工的姓名和部門號和年薪 SELECT last_name, department_id,salary*12*(1+IFNULL(commission_pct,0)) as 年薪 from employees where employee_id = 176# 3. 選擇工資不在 5000 到 12000 的員工的姓名和工資 SELECT last_name, salary from employees where salary not BETWEEN 5000 and 12000# 4. 選擇在 20 或 50 號部門工作的員工姓名和部門號 SELECT last_name, department_id from employees where department_id = 20 or department_id = 50 -- # 5. 選擇公司中沒有管理者的員工姓名及 job_id SELECT last_name,job_id from employees where manager_id is NULL -- # 6. 選擇公司中有獎金的員工姓名,工資和獎金級別 SELECT last_name,salary,commission_pct from employees WHERE commission_pct is not null -- # 7. 選擇員工姓名的第三個字母是 a 的員工姓名 SELECT last_name from employees where last_name like "__a%" -- # 8. 選擇姓名中有字母 a 和 e 的員工姓名 SELECT last_name from employees where last_name like "%a%e%" or last_name like "%e%a%" -- # 9. 顯示出表 employees 表中 first_name 以 'e'結尾的員工信息 SELECT * from employees where first_name like "%e" -- # 10. 顯示出表 employees 部門編號在 80-100 之間 的姓名、職位 select last_name, job_id from employees where department_id BETWEEN 80 and 100 -- # 11. 顯示出表 employees 的 manager_id 是 100,101,110 的員工姓名、職位 select last_name, job_id from employees where manager_id in (100,101,110)

    二、排序(P270)

    題目

  • 查詢員工的姓名和部門號和年薪,按年薪降序 按姓名升序
  • 選擇工資不在 8000 到 17000 的員工的姓名和工資,按工資降序
  • 查詢郵箱中包含 e 的員工信息,并先按郵箱的字節數降序,再按部門號升序
  • 練習

    # 1. 查詢員工的姓名和部門號和年薪,按年薪降序 按姓名升序 SELECT last_name, department_id,salary*12*(1+IFNULL(commission_pct,0)) as 年薪 from employees ORDER BY 年薪 DESC,last_name ASC# 2. 選擇工資不在 8000 到 17000 的員工的姓名和工資,按工資降序 select last_name,salary from employees WHERE salary not BETWEEN 8000 and 17000 ORDER BY salary DESC# 3. 查詢郵箱中包含 e 的員工信息,并先按郵箱的字節數降序,再按部門號升序 SELECT * from employees where email LIKE "%e%" ORDER BY LENGTH(email) DESC, department_id ASC

    三、常見函數(P277)

    題目

  • 顯示系統時間(注:日期+時間)
  • 查詢員工號,姓名,工資,以及工資提高百分之 20%后的結果(new salary)
  • 將員工的姓名按首字母排序,并寫出姓名的長度(length)
  • 做一個查詢,產生下面的結果
    <last_name> earns monthly but wants <salary*3>
  • Dream Salary
    King earns 24000 monthly but wants 72000
  • 使用 case-when,按照下面的條件:
  • jobgrade
    AD_PRESA
    ST_MANB
    IT_PROGC
    SA_REPD
    ST_CLERKE

    產生下面的結果:

    Last_nameJob_idGrade
    kingAD_PRESA

    練習

    # 1. 顯示系統時間(注:日期+時間) SELECT NOW()# 2. 查詢員工號,姓名,工資,以及工資提高百分之 20%后的結果 SELECT employee_id, last_name, salary, salary*1.2 as "New salary" from employees# 3. 將員工的姓名按首字母排序,并寫出姓名的長度(length) SELECT last_name, LENGTH(last_name) from employees ORDER BY SUBSTR(last_name,1,1)# 4. 做一個查詢,產生下面的結果 # <last_name> earns <salary> monthly but wants <salary*3> SELECT CONCAT(last_name,' earns ',salary,' monthly but he wants ',salary*3) as "Dream Salary" from employees# 5. 使用 case-when,按照下面的條件: SELECT last_name,job_id, CASE job_idWHEN 'AD_PRES' THEN 'A'WHEN 'ST_MAN' THEN 'B'WHEN 'IT_PROG' THEN 'C'WHEN 'SA_REP' THEN 'D'WHEN 'ST_CLERK' THEN 'E' END as Gradefrom employees

    四、測試題3(P284)

    題目

  • 查詢員工姓名、入職日期并按入職日期升序
  • 將當前日期顯示成xxxx年xx月xx日
  • 練習

    # 1. 查詢員工姓名、入職日期并按入職日期升序 SELECT last_name,hiredate from employees ORDER BY YEAR(hiredate) ASC# 2. 將當前日期顯示成xxxx年xx月xx日 SELECT DATE_FORMAT(now(),"%Y年%m月%d日")

    五、外連接查詢(P288)

    題目

  • 查詢編號>3 的女神的男朋友信息,如果有則列出詳細,如果沒有,用 null 填充
  • 查詢哪個城市沒有部門
  • 查詢部門名為 SAL 或 IT 的員工信息
  • 練習

    # 1. 查詢編號>3 的女神的男朋友信息,如果有則列出詳細,如果沒有,用 null 填充 select b.name,bo.* from beauty b left join boys bo on b.boyfriend_id=bo.id where b.id>3# 2. 查詢哪個城市沒有部門 select city from departments d RIGHT JOIN locations l on d.location_id=l.location_id where d.department_id is null# 3. 查詢部門名為 SAL 或 IT 的員工信息 SELECT d.*,e.* from departments d left join employees e on d.department_id = e.department_id where department_name in ('SAL','IT')

    六、子查詢(P290+P303)

    題目

  • 查詢和 Zlotkey 相同部門的員工姓名和工資
  • 查詢工資比公司平均工資高的員工的員工號,姓名和工資。
  • 查詢各部門中工資比本部門平均工資高的員工的員工號, 姓名和工資
  • 查詢和姓名中包含字母 u 的員工在相同部門的員工的員工號和姓名
  • 查詢在部門的 location_id 為 1700 的部門工作的員工的員工號
  • 查詢管理者是 King 的員工姓名和工資
  • 查詢工資最高的員工的姓名,要求 first_name 和 last_name 顯示為一列,列名為 姓.名
  • 練習

    # 1. 查詢和 Zlotkey 相同部門的員工姓名和工資 SELECT last_name,salary from employees WHERE department_id in (SELECT department_idfrom employeeswhere last_name='Zlotkey' ) # 2. 查詢工資比公司平均工資高的員工的員工號,姓名和工資。 SELECT employee_id,last_name,salary from employees where salary > (SELECT AVG(salary)FROM employees ) # 3. 查詢各部門中工資比本部門平均工資高的員工的員工號, 姓名和工資(!) SELECT employee_id,last_name,salary from employees e JOIN (SELECT department_id, avg(salary) as agfrom employeesGROUP BY department_id )as dep_ag on e.department_id = dep_ag.department_id WHERE e.salary> dep_ag.ag# 4. 查詢和姓名中包含字母 u 的員工在相同部門的員工的員工號和姓名 select employee_id,last_name from employees WHERE department_id in (SELECT department_idfrom employeesWHERE last_name like "%u%" )# 5. 查詢在部門的 location_id 為 1700 的部門工作的員工的員工號 SELECT employee_id FROM employees e JOIN departments d ON e.department_id = d.department_id WHERE d.location_id=1700# 6. 查詢管理者是 King 的員工姓名和工資 SELECT last_name,salary from employees WHERE manager_id in (SELECT employee_idfrom employeesWHERE last_name = "K_ing" )# 7. 查詢工資最高的員工的姓名,要求 first_name 和 last_name 顯示為一列,列名為 姓.名 SELECT CONCAT(first_name,'.',last_name) as "姓.名" from employees where salary in (SELECT max(salary)from employees )

    相關資料

    尚硅谷的sql教程https://www.bilibili.com/video/av49181542?p=267

    尚硅谷mysql教程配套資料,
    鏈接: https://pan.baidu.com/s/1thTHme-QtXPuowX5fgGNnQ 提取碼: 1ykn

    這里有測試數據 https://blog.csdn.net/GongmissYan/article/details/102937816

    某位大神根據視頻整理的筆記https://cloudlandboy.github.io/myNote/#/backend/mysql/dbandsql

    總結

    以上是生活随笔為你收集整理的SQL基础练习的全部內容,希望文章能夠幫你解決所遇到的問題。

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