• Sql题目精选练习


    1.每日经典sql

     1.1.1 根据三张关系表查询雇员中工资最高的雇员的员工姓名、工资和部门号。

     salary(工资表)        employee(员工表)        department(部门表)

        

    Sql语句: 

     1 SELECT
     2     emp.ename AS '姓名',
     3     sal.sal AS '薪资',
     4     dep.depname AS '部门' 
     5 FROM
     6     employee AS emp
     7     INNER JOIN salary AS sal
     8     INNER JOIN department dep ON dep.salid = sal.salid 
     9     AND dep.empid = emp.empid 
    10     AND sal.sal = ( SELECT max( sal ) FROM salary )
    View Code

      1.1.2 找出各月倒数第3天受雇的所有员工.
      emp(练习表)

    Sql语句:

    1 SELECT
    2     * 
    3 FROM
    4     emp 
    5 WHERE    --关键点:last_day()函数返回日期的最后一天
    6     hiredate = last_day( hiredate ) -2
    View Code

      1.1.3 显示所有员工的姓名,用a替换所有"A"

    Sql语句:

    1 SELECT REPLACE    --replace函数 替换一些字符
    2     ( ename, 'A', 'a' ) name,
    3         job,
    4     hiredate 
    5  FROM
    6     emp;
    View Code

       1.1.4 显示所有员工的姓名、加入公司的年份和月份,按受雇日期所在月排序,若月份相同则将最早年份的员工排在最前面.

     Sql语句:

    1 SELECT
    2     ename,
    3     --t o_char函数是用来截取时间的 to_char ( hiredate, 'yyyy' ) YEAR,
    4     to_char ( hiredate, 'mm' ) months 
    5 FROM
    6     emp 
    7 ORDER BY
    8     months,
    9     YEAR ASC;
    View Code

      1.1.5 显示姓名字段的任何位置包含"A"的所有员工的姓名.

     Sql语句:

    1 SELECT    -- racle instr函数返回要截取的字符串在源字符串中的位置。只检索一次,也就是说从字符的开始到字符的结尾就结束。 
    2     ename 
    3 FROM
    4     emp 
    5 WHERE
    6     instr( ename, 'A', 1 ) > 0;
    View Code

      1.1.6 列出薪金比“SMITH”多的所有员工. --upper函数 小写字符转化成大写的函数

    Sql语句:

    1 SELECT
    2     ename,
    3     sal 
    4 FROM
    5     emp 
    6 WHERE
    7     sal > ( SELECT sal FROM emp WHERE upper( ename ) = 'SMITH' );
    View Code

      1.1.7 列出受雇日期早于其直接上级的所有员工

    Sql语句:

    1 SELECT
    2     e.ename,
    3     m.ename 
    4 FROM
    5     emp e,
    6     emp m 
    7 WHERE
    8     e.mgr = m.empno 
    9     AND ( e.hiredate < m.hiredate );
    View Code

      2.1.1 50道经典的Sql题。

     

      2.1.2 查询“3-105”课程比“6-166”课程成绩高的所有学生的学号

    Sql语句:

    1 SELECT
    2     * 
    3 FROM
    4     SCORE A 
    5 WHERE
    6     A.Cno = '3-105' 
    7     AND A.Degree > ( SELECT B.Degree FROM SCORE B WHERE B.Cno = '6-166' AND A.Sno = B.Sno )
    View Code

      2.1.2 查询平均成绩大于80分的同学的学号和平均成绩  

    Sql语句:

    1 SELECT
    2     Sno '学号',
    3     avg( Degree ) '平均成绩' 
    4 FROM
    5     score 
    6 GROUP BY
    7     Sno 
    8 HAVING
    9     avg( Degree ) > 80
    View Code

      2.1.3 查询所有同学的学号、姓名、选课数、总成绩  

    Sql语句:

     1 SELECT
     2     A.Sno '学号',
     3     A.Sname '姓名',
     4     count( Cno ) '选课数',
     5     sum( B.Degree ) '成绩' 
     6 FROM
     7     student A
     8     INNER JOIN score B ON A.Sno = B.Sno 
     9 GROUP BY
    10     A.Sno
    View Code

      2.1.4 查询姓“李”的老师的个数  

    Sql语句:

    1 SELECT
    2     count( 1 ) 
    3 FROM
    4     teacher A 
    5 WHERE
    6     SUBSTR( A.Tname, 1, 1 ) = ''
    View Code

      2.1.5 查询没学过“李诚”老师课的同学的学号、姓名  --应该有比这个简便的方法但是我想不到

    Sql语句: 

     1 SELECT
     2     * 
     3 FROM
     4     student 
     5 WHERE
     6     sno NOT IN (
     7 SELECT
     8     S.sno 
     9 FROM
    10     student S
    11     INNER JOIN score SC ON S.Sno = SC.Sno 
    12     AND Sc.Cno IN ( SELECT B.Cno FROM teacher A INNER JOIN course B ON A.Tno = B.Tno AND A.Tname = '李诚' ) 
    13     )
    View Code

      2.1.6 查询学过“3-105”并且也学过编号“3-245”课程的同学的学号、姓名;

    Sql语句: 

     1 --第一种方式
     2 SELECT
     3     Sno '学号',
     4     Sname '姓名' 
     5 FROM
     6     student 
     7 WHERE
     8     Sno IN (
     9 SELECT
    10     A.Sno 
    11 FROM
    12     score A 
    13 WHERE
    14     A.Cno = '3-105' 
    15     AND A.Sno IN ( SELECT B.Sno FROM score B WHERE B.Cno = '3-245' ) 
    16     )
    17 --第二种方式
    18 SELECT
    19     stu.Sno '学号',
    20     stu.Sname '姓名' 
    21 FROM
    22     student AS stu,
    23     score sc,
    24     score c 
    25 WHERE
    26     stu.Sno = sc.Sno 
    27     AND sc.Sno = c.Sno 
    28     AND sc.Cno = '3-105' 
    29     AND c.Cno = '3-245'
    View Code
  • 相关阅读:
    你的代码真的很健壮吗
    GAE 博客——B3log Solo 0.1.1 发布预告
    GAE 博客——B3log Solo 0.1.1 发布了!
    GAE 博客——B3log Solo 0.1.1 发布了!
    使用logcxx库和boost库构建系统日志的格式化输出
    Simple Hierarchical clustering in Python 2.7 using SciPy
    将python3.1+pyqt4打包成exe
    Installation — SIP 4.14.2 Reference Guide
    PyQt 维基百科,自由的百科全书
    沙湖王 py行者
  • 原文地址:https://www.cnblogs.com/joeyJss/p/11094366.html
Copyright © 2020-2023  润新知