MySQL---表的查詢實戰OJ
–查詢工資高於500或崗位為MANAGER的僱員,同時還要滿足他們的姓名首字母為大寫的J
select * from emp where (sal>100 or job='manager') and ENAME LIKE 'j%';
–按照部門號升序而僱員的工資降序排序
SELECT ename,deptno ,sal from emp order by deptno asc , sal desc;
–使用年薪進行排序
select sal*12 as annual_salary from emp desc;
–顯示工資最高的員工的名字和工作崗位
select ename,job,max(sal) from emp;
–顯示工資高於平均工資的員工資訊
select * from emp where sal > (select avg(sal) from emp);
–顯示每個部門的平均工資和最高工資
select deptno,avg(sal),max(sal) from emp group by deptno;
–顯示平均工資低於2000的部門號和和它的平均工資
select deptno,avg(sal) from emp group by deptno having avg(sal)<2000;
–顯示每種崗位的僱員總數,平均工資
select count(*),avg(sal) from emp group by deptno;
–要emp表中的deptno = dept表中的deptno欄位的記錄
select emp.deptno,dept.deptno from emp,dept where emp.deptno=dept.deptno;
–顯示部門號為10的部門名,員工名和工資
select dname,sal,ename from emp,dept where emp.deptno=dept.deptno and emp.deptno=10;
–顯示各個員工的姓名,工資,及工資級別
select ename,sal,grade from emp,salgrade where emp.sal between salgrade.losal and salgrade.hisal;
–列出部門名稱和這些部門的員工資訊,同時列出沒有員工的部門
select dept.dname,emp.*
from dept left join emp on
dept.deptno=emp.deptno;
–實戰OJ6
–牛客:批量插入資料
insert into actor (actor_id,first_name,last_name,last_update)
values(1,'PENELOPE','GUINESS',datetime('2006-02-15 12:34:33')),(2,'NICK','WAHLBERG',datetime('2006-02-15 12:34:33'));
–牛客:找出所有員工當前(to_date=‘9999-01-01’)具體的薪水salary情況,對於相同的薪水只顯示一次,並按照 逆序顯示
select distinct salary from salaries where to_date
like '9999-01-01' order by salary desc;
–牛客:查詢晚入職員工的所有資訊
select emp_no,birth_date,first_name,last_name,gender,hire_date
from employees
order by hire_date desc
limit 1 offset 0;
–牛客:查詢入職員工時間排名倒數第三的員工所有資訊
select * from employees order by hire_date desc limit 1 offset 2;
–牛客:查詢薪水漲幅超過15次的員工號emp_no以及其對應的漲幅次數t
select emp_no,count(salary) as t from salaries group by emp_no
having count(salary)>15;
–牛客:獲取所有部門當前manager的當前薪水情況,給出dept_no, emp_no以及salary,當前表示 to_date=‘9999-01-01’
select dept_no,dept_manager.emp_no,salary
from dept_manager,salaries
where dept_manager.emp_no=salaries.emp_no
and salaries.to_date like '9999-01-01'
and
dept_manager.to_date like '9999-01-01'
;
–牛客:從titles表獲取按照title進行分組,每組個數大於等於2,給出title以及對應的數目t
select title,count(title) as t
from titles group by title having count(title)>=2
;
–leetcode: duplicate-emails leetcode: big-countries
select Email from Person
group by Email having count(Email)>1;
–leetcode: nth-highest-salary
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
RETURN (
# Write your MySQL query statement below.
select Salary from Employee order by salary desc limit 1 offset n-1
);
END
–leetcode: big-countries
select name,population,area from World
where population>25000000 or area> 3000000;
–牛客:查詢字串’10,A,B’ 中逗號’,'出現的次數cnt
select (length('10,A,B')-length(replace('10,A,B',',','')))/length(',') as cnt;
實戰OJ8
–查詢所有員工入職時候的薪水情況,給出emp_no以及salary, 並按照emp_no進行逆序
select employees.emp_no,salary
from employees,salaries
where (employees.emp_no=salaries.emp_no and employees.hire_date=salaries.from_date)
order by employees.emp_no desc;
–內連線
select ename,dname from emp inner join dept on dept.deptno=emp.deptno and ename='smith';
–外連線 查詢所有學生的成績,如果這個學生沒有成績,也要將學生的個人資訊顯示出來
create table stu(id int,name varchar(20));
insert into stu values(1,'maia'),(2,'peter'),(3,'nancy');
create table exam (id int,grade int);
insert into exam values(1,56),(2,34),(3,68);
–對stu表和exam表聯合查詢,把所有的成績都顯示出來,即使這個成績沒有學生與它對應,也要顯示出來
select * from stu right join exam on stu.id=exam.id;
–建立主鍵索引‘
create table user1(id int primary key,name varchar(10));
insert into user1 values(1,'jack'),(2,'tully');
–建立唯一鍵索引
create table user4(id int unique,name varchar(20));