SQL入门题集及学习笔记

本篇为SQL自学过程中遇到的典型例题及自己的学习笔记,会随着学习进程不断更新,题目都借鉴自网络或书籍,仅用作个人学习。由于水平实在有限,不免产生谬误,欢迎读者多多批评指正。如需要转载请与博主联系,谢谢

SQL入门习题记录


笔试命令类

  • 基础语句例题1:

    Q:Give the name and the per capita GDP for those countries with a population of at least 200 million.
    A:SELECT name,gdp/population FROM world WHERE population > 200000000
    Q:Show the countries which have a name that includes the word 'United‘
    A:SELECT name FROM world WHERE name LIKE '%United%'
    Q:Show the countries that are big by area (more than 3 million) or big by population (more than 250 million) but not both. Show name, population and area.
    A:SELECT name,population,area FROM world WHERE area > 3000000 xor population > 250000000
    Q:Show the name and population in millions and the GDP in billions(to 2 decimal places) for the countries of the continent 'South America'.
    A:SELECT name,ROUND(population/1000000,2),ROUND(gdp/1000000000,2) FROM world WHERE continent = 'South America'
    Q:Show the name and the capital where the first letters of each match. Don't include countries where the name and the capital are the same word.
    A:SELECT name,capital FROM world WHERE LEFT(name,1) = LEFT(capital,1) and not name = capital (用<>应该怎么写呢)
    Q:Equatorial Guinea and Dominican Republic have all of the vowels (a e i o u) in the name. They don't count because they have more than one word in the name.Find the country that has all the vowels and no spaces in its name.
    A:SELECT name FROM world WHERE name LIKE '%a%' AND name LIKE '%e%' AND name LIKE '%i%' AND name LIKE '%o%' AND name LIKE '%u%' AND name NOT LIKE '% %' (更简便的写法是什么)

  • 基础语句例题2:

    Q:Show all details (yr, subject, winner) of the Literature prize winners for 1980 to 1989 inclusive.
    A:SELECT yr,subject,winner FROM nobel WHERE subject = 'Literature' And yr BETWEEN 1980 AND 1989
    Q:Show all details of the presidential winners:Theodore Roosevelt,Woodrow Wilson,Jimmy Carter,Barack Obama
    A:SELECT * FROM nobel WHERE winner IN ('Theodore Roosevelt','Woodrow Wilson','Jimmy Carter','Barack Obama')
    Show the winners with first name John
    A:SELECT winner FROM nobel WHERE winner LIKE 'John%'
    Q:Show the year, subject, and name of Physics winners for 1980 together with the Chemistry winners for 1984.
    A:SELECT * FROM nobel WHERE (subject = 'Physics' AND yr = 1980) OR (subject = 'Chemistry' AND yr = 1984)
    Q:Show the year, subject, and name of winners for 1980 excluding Chemistry and Medicine
    A:SELECT * FROM nobel WHERE subject <> 'Chemistry' AND subject <> 'Medicine' AND yr =1980
    Q:Find all details of the prize won by PETER GRÜNBERG,Non-ASCII characters
    A:SELECT * FROM nobel WHERE winner LIKE 'PETER GR_NBERG' (_可以代表需要匹配的任意未知字符)
    Q:List the winners, year and subject where the winner starts with Sir. Show the the most recent first, then by name order.
    A:SELECT winner, yr , subject FROM nobel WHERE winner LIKE 'Sir%' ORDER BY yr desc, winner ('Sir%'为以该字符串开头,yr desc 则按年份降序排列)
    Q:The expression subject IN ('Chemistry','Physics') can be used as a value - it will be 0 or 1. Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.
    A:-(这个题不太会做,要再研究一下。。)

笔试客观题

  • 检索所有比“王华”年龄大的学生姓名、年龄和性别。正确的SELECT语句是(A):

    • A. SELECT SN,AGE,SEX FROM S WHERE AGE>(SELECT AGE FROM S WHERE SN=“王华”)
    • B. SELECT SN,AGE,SEX FROM S WHERE SN=“王华”
    • C. SELECT SN,AGE,SEX FROM S WHERE AGE>(SELECT AGE WHERE SN=“王华”)
    • D. SELECT SN,AGE,SEX FROM S WHERE AGE>王华.AGE
      解析:简单嵌套查询,先处理括号内、再处理括号外的查询语句。
  • 一张学生成绩表score,部分内容如下:
    name course grade
    张三 操作系统 67
    张三 数据结构 86
    李四 软件工程 89
    用一条SQL 语句查询出每门课都大于80 分的学生姓名,SQL语句实现正确的是:(A)

    • A. Select distinct name from score where name not in(Select name from score where grade <= 80)
    • B. Select distinct name from score where name in(Select name from score where grade <= 80)
    • C. Select name from score where name not in(Select name from score where grade <= 80)
    • D. Select name from score where name in(Select name from score where grade <= 80)
  • SQL中,下面对于数据定义语言DDL描述正确的是(D):

    • A. DDL关心的是数据库中的数据
    • B. 联盟链
    • C. 控制对数据库的访问
    • D. 定义数据库的结构
      解析:DDL为数据定义语言(CREATE/DROP/ALTER);DML为数据操纵语言(SELECT/INSERT/UPDATE/DELETE);DCL为数据控制语言(COMMIT/ROLLBACK/GRANT/REVOKE)
  • select语句完整语法:
    select 目标表的列名或列表达式序列
    from 基本表名和(或)视图序列
    [where 行条件表达式]
    [group by 列名序列] [having 组条件表达式]
    [order by 列名[asc | desc]],则sql语句的执行顺序是:(B)

    • A. 1),3),4),2), 5)
    • B. 2),3),4),1) ,5)
    • C. 2),4),3),1) ,5)
    • D. 2),3),5),1) ,4)
      解析:SQL写的顺序select-from-where-groupby-having-orderby-limit;执行顺序from-where-groupby-having-select-orderby-limit
  • 在SQL中语法规范中,having子句的使用下面描述正确的是:(AC)

    • A. having子句即可包含聚合函数作用的字段也可包括普通的标量字段
    • B. 使用having的同时不能使用where子句
    • C. having子句必须于group by 子句同时使用,不能单独使用
    • D. 使用having子句的作用是限定分组条件
    • E. Having子句和where子句是等同的
    • F. 如果select语句中没有聚合函数的使用,就不能使用having子句
      解析:D中限定分组条件的是group by,having用在分组后,对分组的结果过滤;F中没有聚合函数的使用也可以用having过滤。
  • 快件信息表(waybillinfo)中存储了快件的所有操作信息,请找出在中山公园网点,异常派送(optype='异常派件')次数超过3次的快件(waybillno),正确的sql为()

    • A. select waybillno, count(*) from waybillinfo where zonecode='中山公园' and optype='异常派件' and count(waybillno) >3
    • B. select waybillno, count() from waybillinfo where zonecode='中山公园' and optype='异常派件' order by waybillno having count() > 3
    • C. select waybillno, count() from waybillinfo where zonecode='中山公园' and optype='异常派件' having count() > 3
    • D. select waybillno from waybillinfo where zonecode='中山公园' and optype='异常派件' group by waybillno having count(*) > 3
      *解析:having 是对 group by后的数据进行筛选过滤,必须要有group by才能用having。而A中COUNT函数不能用在WHERE之后。

参考资料:

  1. https://sqlzoo.net/wiki/SELECT_from_Nobel_Tutorial SQLZOO
  2. https://www.nowcoder.com/ 牛客网
  3. https://www.nowcoder.com/discuss/95812 SQL面经汇总
  4. https://blog.csdn.net/XindiOntheWay/article/details/82697988 SQL面经
  5. http://www.dscademy.com/languages/sql/ LionKing数据科学专栏
原文地址:https://www.cnblogs.com/liugd-2020/p/13390146.html