由于感觉数据库难度可能暂时面试用不到,就先不刷啦
20240408
1.从不订购的客户
SELECT Name AS Customers
FROM Customers C LEFT JOIN Orders O
ON C.Id = O.CustomerId
WHERE CustomerId is null
select customers.name as 'Customers'
from Customers
where Customers.id not in ( select customerid from Orders );
耗时久
20240409
184.部门工资最高的员工
select Department.name AS 'Department',Employee.name AS 'Employee',Salaryfrom Employee join Department on Employee.departmentID=Department.id where (Employee.departmentID,Salary) in (select DepartmentID,MAX(Salary) from Employee group by departmentID );
SQL的执行顺序是:FROM–WHERE–GROUP BY–HAVING–SELECT–ORDER BY
185.部分工资前三高的员工
耗时久
select d.name as 'Department',e1.name as 'Employee',e1.salary
from Employee e1 join Department d on e1.departmentId=d.id
where 3>( select count(distinct e2.salary)
from Employee e2
where e2.salary>e1.salary
and e1.departmentId=e2.departmentId )
耗时较短
select d.name as department, e1.name as employee,e1.salary as salary
from employee e1 inner join department d on e1.DepartmentId = d.id where e1.name
in ( select e3.namefrom employee e3left join employee e on e3.DepartmentId = e1.departmentid and e3.Salary < e1.salary and e3.id != e1.idgroup by 1 having count(distinct e1.salary) < 3 )
20240411
197.上升的温度
select a.id from Weather as a,Weather as b where datediff(a.recordDate,b.recordDate) = 1 and a.Temperature >b.Temperature;
select a.ID from weather as a cross join weather as b
on timestampdiff(day, a.recordDate, b.recordDate) = -1
where a.Temperature > b.Temperature;
511.游戏玩法分析I
select A.player_id,Min(event_date) as first_login from Activity A group by A.player_id;
这个不加别名A会运行超时,原因:
550.游戏玩法分析IV
select ifnull(round(count(distinct(Result.player_id))/count(distinct(Activity.player_id)),2),0)
as fraction
from(
select Activity.player_id as player_id
from(
select player_id,date_ADD(Min(event_date),interval 1 day) as second_date
from Activity
group by player_id ) as Expected,Activity
where Activity.event_date=Expected.second_date
and Activity.player_id=Expected.player_id ) as Result,Activity
570.至少有5名直接下属
select Manager.Name as Name
from Employee as Manager join Employee as Report
on Manager.id=Report.ManagerID
group by Manager.id
having count(Report.id)>=5
知识点学习:
-
where后接的是用来筛选结果的查询条件
-
存储过程包含很多行SQL语句,但在客户机调用存储过程时,网络中只要传送调用存储过程的语句,而无需在网络中发送很多行代码,减少了网络流量,特别是大型、复杂的数据处理,存储过程无需将中间结果集送回客户机,只要发送最终结果。
-
inner join时只会对非NULL的记录做join
-
数据控制语言DCL,主要用于对用户权限的授权和回收;DDL主要的命令有CREATE、ALTER、DROP等,大多在建立表时使用;DML包括SELECT、UPDATE等,对数据库里的数据进行操作;数据库事务包括COMMIT、ROLLBACK等,主要用于对事务的提交、回收和设置保存点。
-
drop>trustcate>delete区别:
- 1:处理效率:drop>trustcate>delete
- 2:删除范围:drop删除整个表(结构和数据一起删除);trustcate删除全部记录,但不删除表结构;delete只删除数据
- 3:高水位线:delete不影响自增ID值,高水线保持原位置不动;trustcate会将高水线复位,自增ID变为1。
-
MySQL中ALTER TABLE命令可以修改数据表的表名或数据表的字段。但是接不同后缀意义不同,比如: 要修改表名或索引名时,可以用RENAME函数;当然RENAME也可以更改列名,但是后面要加TO,且它只会更改列的名字,并不更改定义。 要修改字段定义和名称,可以用MODIFY或CHANGE函数。但是MODIFY只改字段定义,不改名字;CHANGE是两个都可以修改。 要修改字段默认值,可以用ALTER 字段名 SET DEFULT 更改值。 所以根据题意,要修改表名,只能用RENAME函数,因此A正确;
20240412
2356.每位教师所教授的科目种类
select teacher_id,count(distinct subject_id) as cnt from teacher group by teacher_id
1148.文章浏览I
select distinct author_id as id from Views where author_id=viewer_id order by id
1633.各赛事的用户注册率
select contest_id,round(count(user_id)*100/(select count(*) from users),2) percentage
from Register
group by contest_id
order by percentage desc,contest_id
- ucase是转换成大写函数
- 关系代数有五个基础运算符,这五个基础运算符能派生出其他组合运算符。它们分别是:
- 选择(σ, selection)、投影(π, projection)、叉乘(x, cross-product)、
- 差(-, set-difference)和并(υ, union)
- 它们和SQL语句的对应关系为:
- 选择(σ, selection)相当于SQL语句中的where,表示选出满足一定条件的行。 如:σ rating>8 (S2)相当于 select * from S2 where rating>8;
- 投影(π, projection)相当于SQL语句中的select。。。distinct, 表示选择哪些列。注意:投影是会去重的! 如:π sname,rating (σ rating>8 (S2))相当于 select sname, rating from S2 where rating>8;
- 叉乘(x, cross-product)相当于SQL语句中的from,表示穷举所有集合两边元素的组合量 如: AxB 相当于 select * from A, B; 注意:叉乘时两个集合不能有重名列
- 差(-, set-difference)R-S返回所有在R中而不在S中的元组
- 并(υ, union)RυS返回包含在R中或在S中的所有元组
- SQL语言的组成部分有:数据定义语言、数据处理语言、数据控制语言、数据库事务。数据字典是关于数据库中数据的描述,是元数据,而不是数据本身。