美文网首页
sql面试练习题:代码

sql面试练习题:代码

作者: 鲸鱼酱375 | 来源:发表于2019-06-21 23:30 被阅读0次

题目来源

1

q1
select top (3) author_name from authors as a
right join books as b on book_name
group by a.author_name
order by b.sold_copies;

正确答案:

SELECT authors.author_name, SUM(books.sold_copies) AS sold_sum
FROM authors
JOIN books
ON books.book_name = authors.book_name
GROUP BY authors.author_name
ORDER BY sold_sum DESC
LIMIT 3;

2

Q2
SELECT COUNT(*) FROM
  (SELECT user_id, COUNT(event_date_time) AS image_per_user
  FROM event_log
  GROUP BY user_id) AS image_per_user
WHERE image_per_user < 2000 AND image_per_user > 1000;

3

Q3
select e.department_name 
from employees as e join salaries as s on employee_id
group by e.department_name
having avg(s.salary)<500;

正确答案:

SELECT department_name, AVG(salaries.salary) AS avg_salaries
FROM employees
JOIN salaries
ON employees.employee_id = salaries.employee_id
GROUP BY department_name
HAVING AVG(salaries.salary) < 500;

4.Delete duplicate data from table only first data remains constant.

image.png
DELETE M1 
From managers M1, managers M2 
Where M2.Name = M1.Name AND M1.Id>M2.Id; 

5.Find the Name of Employees.

Finding the name of Employees where First Name, Second Name and Last Name is given in table. Some Name is missing such as First Name, Second Name and may be Last Name. Here we will use COALESCE() function which will return first Non Null values.


image.png
select id, coalesce(fname,sname,lname) from employees;

6.Find the Employees who hired in the Last n months.

Finding the Employees who have been hire in the last n months. Here we get desire output by using TIMESTAMPDIFF() mysql function.


image.png
select fname,lname, timestampdiff(hiredate) as month from employees;

正确答案:

Select *, TIMESTAMPDIFF (month, Hiredate, current_date()) as DiffMonth 
From employees
Where TIMESTAMPDIFF (month, Hiredate, current_date()) 
Between 1 and 5 Order by Hiredate desc; 

6.Find the Employees who hired in the Last n days.

Finding the Employees who have been hire in the last n days. Here we get desire output by using DATEDIFF() mysql function.


image.png
select *, datediff(day,hiredate,current_date()) as diffday from employees;

正确答案:

Select *, DATEDIFF (current_date(), Hiredate) as DiffDay 
From employees
Where DATEDIFF (current_date(), Hiredate) between 1 and 100 order by Hiredate desc; 

7.找出每月最后一天的sales

image.png
Rank () over (partition by Month(date) order by Day(date) desc) 然后选 rank = 1

参考

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

select char_length('A,10,B')- char_length(replace('10,A,B',',',''));

相关文章

  • sql面试练习题:代码

    题目来源 1 正确答案: 2 3 正确答案: 4.Delete duplicate data from table...

  • 第4关《从零学会SQL:复杂查询》练习题答案

    这是《从零学会sql》系列课程第4节课《复杂查询》的练习题,也是常考常考的面试题。 题目来自sqlzoo的子查询题...

  • MySQL 练习题

    01第一天20180703 【SQL练习】经典SQL练习题 02第二天20180704 sql语句练习50题(My...

  • bWAPP学习笔记 - A1 Injection (二)

    SQL Injection (GET/Search) 手注练习题 (^_^) Level: Low 先输入单引号'...

  • 13.runloop 练习理解

    问题 1.练习题1当前代码 在主队列中执行 2.练习题2当前代码在主线程当中运行 3.练习题3当前代码在主线程当中...

  • LeetCode-SQL-nine

    Leetcode-sql-nine 本文中主要是介绍LeetCode中关于SQL的练习题,从易到难,循序渐进。文中...

  • LeetCode-SQL-five

    LeetCode-SQL-five 本文中主要是介绍LeetCode中关于SQL的练习题,从易到难,循序渐进。文中...

  • LeetCode-SQL-four

    LeetCode-SQL-four 本文中主要是介绍LeetCode中关于SQL的练习题,从易到难,循序渐进。文中...

  • mysql练习题

    SQL练习题目来源 https://www.nowcoder.com/activity/oj[https://ww...

  • 【SQL笔记】sql 练习题

    1.用一条SQL 语句 查询出每门课都大于80 分的学生姓名 name kecheng fenshu张三 ...

网友评论

      本文标题:sql面试练习题:代码

      本文链接:https://www.haomeiwen.com/subject/vkspqctx.html