SQL笔试题

SQL实战技巧
1.用一条SQL语句 查询出每门课都大于80分的学生姓名
name kecheng fenshu
张三 语文 81
张三 数学 75
李四 语文 76
李四 数学 90
王五 语文 81
王五 数学 100
王五 英语 90

A: select distinct name from table where name not in (select distinct name from table where fenshu<=80)

2.学生表 如下:
自动编号 学号 姓名 课程编号 课程名称 分数
1 2005001 张三 0001 数学 69
2 2005002 李四 0001 数学 89
3 2005001 张三 0001 数学 69
删除除了自动编号不同,其他都相同的学生冗余信息

A: delete tablename where 自动编号 not in(select min(自动编号) from tablename group by 学号,姓名,课程编号,课程名称,分数)
一个叫department的表,里面只有一个字段name,一共有4条纪录,分别是a,b,c,d,对应四个球对,现在四个球对进行比赛,用一条sql语句显示所有可能的比赛组合.
你先按你自己的想法做一下,看结果有我的这个简单吗?
答:select a.name, b.name
from team a, team b
where a.name < b.name

请用SQL语句实现:从TestDB数据表中查询出所有月份的发生额都比101科目相应月份的发生额高的科目。请注意:TestDB中有很多科目,都有1-12月份的发生额。
AccID:科目代码,Occmonth:发生额月份,DebitOccur:发生额。
数据库名:JcyAudit,数据集:Select * from TestDB
答:select a.*
from TestDB a
,(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID='101' group by Occmonth) b
where a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur
************************************************************************************
面试题:怎么把这样一个表儿
year month amount
1991 1 1.1
1991 2 1.2
1991 3 1.3
1991 4 1.4
1992 1 2.1
1992 2 2.2
1992 3 2.3
1992 4 2.4
查成这样一个结果
year m1 m2 m3 m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4

答案一、
select year,
(select amount from aaa m where month=1 and m.year=aaa.year) as m1,
(select amount from aaa m where month=2 and m.year=aaa.year) as m2,
(select amount from aaa m where month=3 and m.year=aaa.year) as m3,
(select amount from aaa m where month=4 and m.year=aaa.year) as m4
from aaa group by year

这个是ORACLE 中做的:
select * from (select name, year b1, lead(year) over
(partition by name order by year) b2, lead(m,2) over(partition by name order by year) b3,rank()over(
partition by name order by year) rk from t) where rk=1;


作者:不详 发文时间:2003.05.29 10:55:05

说明:复制表(只复制结构,源表名:a 新表名:b)

SQL: select * into b from a where 1<>1

说明:拷贝表(拷贝数据,源表名:a 目标表名:b)

SQL: insert into b(a, b, c) select d,e,f from b;

说明:显示文章、提交人和最后回复时间

SQL: select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b

说明:外连接查询(表名1:a 表名2:b)

SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

说明:日程安排提前五分钟提醒

SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5

说明:两张关联表,删除主表中已经在副表中没有的信息

SQL:

delete from info where not exists ( select * from infobz where info.infid=infobz.infid )


将n1=1或者n2=2的记录查出来,再将n1=1并且n2=2的排在最前面按id升序,然后是其它数据(其它数据不要求排序)

id n1 n2 n3
---------------------------------------------
1 1 1 a
2 1 2 2

SQL:通过cases when else end 增加一个虚列,用于排序n1=1 and n2=2 的记录在前面,然后再按ID排。
select id,n1,n2,n3,order_num from(
select id,n1,n2,n3,cases when n1=1 and n2=2 then 2 else 1 end order_num from t_table where n1=1 or n2=2) order by order_num desc,id;
### SQL 笔试题目及答案解析 以下是基于提供的参考资料整理的经典 SQL 笔试题及其答案解析: #### 题目 21 **问题**: 查询部门编号为 1 的员工姓名和工资。 ```sql SELECT name, salary FROM employees WHERE department_id = 1; ``` 此查询通过 `WHERE` 子句筛选出部门编号为 1 的记录,并返回对应的员工姓名 (`name`) 和工资 (`salary`) 列[^1]。 --- #### 题目 22 **问题**: 统计每个部门的平均工资并按降序排列。 ```sql SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id ORDER BY avg_salary DESC; ``` 该语句利用聚合函数 `AVG()` 计算各部门的平均工资,同时使用 `GROUP BY` 对数据分组,并通过 `ORDER BY` 实现降序排序。 --- #### 题目 23 **问题**: 查找工资高于所有员工平均工资的员工信息。 ```sql SELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM employees); ``` 子查询 `(SELECT AVG(salary) FROM employees)` 返回所有员工的平均工资,外层查询则筛选出工资大于该值的员工记录。 --- #### 题目 24 **问题**: 找到入职日期最早的员工姓名和入职时间。 ```sql SELECT name, hire_date FROM employees WHERE hire_date = (SELECT MIN(hire_date) FROM employees); ``` 这里使用了嵌套查询中的 `MIN()` 函数来获取最早入职的时间,并匹配相应的员工信息。 --- #### 题目 25 **问题**: 显示每个职位的总人数以及对应职位名称。 ```sql SELECT job_title, COUNT(*) AS total_employees FROM jobs j JOIN employees e ON j.job_id = e.job_id GROUP BY job_title; ``` 上述代码展示了如何通过连接 (`JOIN`) 表格实现跨表查询,最终统计每种职位的人数。 --- #### 题目 26 **问题**: 获取薪资最高的前五名员工的信息。 ```sql SELECT * FROM employees ORDER BY salary DESC LIMIT 5; ``` 这条命令按照薪水从高到底排序,并限制结果集只显示前五个条目。 --- #### 题目 27 **问题**: 删除没有分配任何项目的员工记录。 ```sql DELETE FROM employees WHERE employee_id NOT IN (SELECT DISTINCT(employee_id) FROM projects); ``` 这里的逻辑是先找出参与过项目的所有员工 ID ,再删除那些不在列表里的员工记录。 --- #### 题目 28 **问题**: 更新某位特定员工的邮箱地址。 假设要更新的是员工ID为'101'的新邮件地址 'new.email@example.com' ```sql UPDATE employees SET email='new.email@example.com' WHERE employee_id=101; ``` 这是标准的数据修改操作语法。 --- #### 题目 29 **问题**: 插入一条新雇员的数据至数据库中。 假设有如下字段需填写:employee_id(唯一), name, salary, department_id. ```sql INSERT INTO employees (employee_id, name, salary, department_id) VALUES ('102', 'John Doe', 5000, 2); ``` 这是一次典型的插入动作示范。 --- #### 题目 30 **问题**: 将两个表格的内容联合起来展示,即使某些键不存在于另一方也应呈现出来(全外联接 Full Outer Join)。 如果存在两张分别存储不同部分客户资料的表A与B,则可以这样写: ```sql SELECT A.*, B.* FROM table_A A FULL OUTER JOIN table_B B ON A.key_column = B.key_column; ``` 注意并非所有的DBMS都支持FULL OUTER JOIN,在不兼容的情况下可能需要用UNION模拟. --- ### 总结 以上仅列举了一部分内容作为示例。更多深入练习可参照其他区间内的习题集合[^2]^[]^。对于准备技术面试或者提升SQL技能的人来说,这些案例非常有价值[^3]^[]^.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值