练习SQL利器,牛客网SQL实战题库,49~56题

49.针对库中的所有表生成select count(*)对应的SQL语句

SELECT "select count(*) from "||name||";" AS cnts  FROM sqlite_master
WHERE type='table'

注:在SQLite系统表sqlite_master中可以获得所有表的索引,其中name表示表的名字,type=‘table’表示当前查找的是表,而type的值永远都是table。

50.将employees表中的所有员工的last_name和first_name通过(')连接起来。

SELECT last_name||"'"||first_name AS name
FROM employees

51.查找字符串'10,A,B'中逗号','出现的次数cnt

题目描述:查找字符串'10,A,B' 中逗号','出现的次数cnt。

SELECT length('10,A,B') - length(replace('10,A,B',",",""))

52.获取Employees中的first_name

SELECT first_name
FROM employees
ORDER BY SUBSTR(first_name,length(first_name)-1,2)

注:

解释来自

substr函数的用法,取得字符串中指定起始位置和长度的字符串 ,默认是从起始位置到结束的子串。

substr( string, start_position, [ length ] ) substr('目标字符串',开始位置,长度)
如:
substr('This is a test', 6, 2) would return 'is'
substr('This is a test', 6) would return 'is a test'
substr('TechOnTheNet', -3, 3) would return 'Net'
substr('TechOnTheNet', -6, 3) would return 'The'select substr('Thisisatest', -4, 2) value from dual

53.按照dept_no进行汇总

SELECT dept_no,group_concat(emp_no) AS employees
FROM dept_emp
GROUP BY dept_no

54.查找排除当前最大、最小salary之后的员工的平均工资avg_salary

image
SELECT AVG(salary) AS avg_salary
FROM salaries
WHERE to_date = '9999-01-01'
AND salary NOT IN (SELECT MAX(salary) FROM salaries)
AND salary NOT IN (SELECT MIN(salary) FROM salaries)

55.分页查询employees表,每5行一页,返回第2页的数据

SELECT *
FROM employees
LIMIT 5,5

56.获取所有员工的emp_no

SELECT de.emp_no, de.dept_no, eb.btype, eb.recevied
FROM dept_emp AS de LEFT JOIN emp_bonus AS eb 
ON de.emp_no = eb.emp_no

注:神题~~没找到emp_bonus表,还是看了讨论才知道的

你可能感兴趣的:(练习SQL利器,牛客网SQL实战题库,49~56题)