1. 程式人生 > >【資料庫】MySQL相關操作

【資料庫】MySQL相關操作

--批量插入資料
insert into 
actor (actor_id, first_name, last_name, last_update)
values 
(1,'PENELOPE','GUINESS','2006-02-15 12:34:33'),(2,'NICK','WAHLBERG','2006-02-15 12:34:33');

--找出所有員工當前(to_date='9999-01-01')具體的薪水salary情況,對於相同的薪水只顯示一次,並按照逆序顯示
select distinct salary from salaries 
where to_date='9999-01-01' order by salary desc;

--查詢最晚入職員工的所有資訊
select *from employees order by hire_date desc limit 0,1;

select *from employees where hire_date in (select max(hire_date) from employees);

select *from employees where hire_date = (select max(hire_date) from employees);

--查詢入職員工時間排名倒數第三的員工所有資訊
select *from employees order by hire_date desc limit 2,1;

--查詢薪水漲幅超過15次的員工號emp_no以及其對應的漲幅次數t
select emp_no, count(emp_no) as t from salaries group by emp_no having t>15;

--獲取所有部門當前manager的當前薪水情況,給出dept_no, emp_no以及salary,當前表示to_date='9999-01-01'
select dept_manager.dept_no, dept_manager.emp_no, salaries.salary
from dept_manager, salaries
where dept_manager.emp_no = salaries.emp_no
and dept_manager.to_date = '9999-01-01'
and salaries.to_date = '9999-01-01';

--從titles表獲取按照title進行分組,每組個數大於等於2,給出title以及對應的數目t
select title, count(emp_no) as t from titles group by title having t>=2;

--Write a SQL query to find all duplicate emails in a table named Person.(編寫一個SQL查詢來查詢名為“人”的表中的所有重複郵件)
select email from person group by emial having count(email)>1;

--Write a SQL solution to output big countries' name, population and area.(編寫一個SQL解決方案,輸出大國的名字、人口和地區)
select name, population, area from world where area>3000000 and population>25000000;

select name, population, area from world where area>3000000
union
select name, population, area from world where population>25000000;


--Write a SQL query to get the nth highest salary from the Employee table(編寫SQL查詢,從僱員表中獲得第N個最高工資)
set M=N-1;
select distinct salary from Employee order by desc limit M,1;