select *
from employees
order by hire_date desc limit 1;
select * from employees
order by hire_date desc limit 2,1;
select s.*,d.dept_no
from salaries as s inner join dept_manager as d
on s.to_date='9999-01-01'
and d.to_date='9999-01-01'
and s.emp_no=d.emp_no
select e.last_name,e.first_name,d.dept_no
from employees as e inner join dept_emp as d
on e.emp_no=d.emp_no
select e.last_name,e.first_name,d.dept_no
from employees as e left join dept_emp as d
on e.emp_no=d.emp_no
select e.emp_no,s.salary
from employees as e inner join salaries as s
on e.emp_no=s.emp_no
and e.hire_date=s.from_date
order by e.emp_no desc
select emp_no,count(emp_no)as t
from salaries
group by emp_no
having t>15
select distinct salary
from salaries
where to_date='9999-01-01'
order by salary desc
select d.dept_no,d.emp_no,s.salary
from dept_manager as d,salaries as s
where d.emp_no=s.emp_no
and d.to_date='9999-01-01'
and s.to_date='9999-01-01'
select emp_no
from employees
where emp_no not in (select emp_no from dept_manager)
select dp.emp_no,dm.emp_no
from dept_emp as dp inner join dept_manager as dm
on dp.dept_no=dm.dept_no
and dp.emp_no<>dm.emp_no
and dm.to_date='9999-01-01'
and dp.to_date='9999-01-01'
select d.dept_no,d.emp_no,max(s.salary)
from dept_emp as d inner join salaries as s
on d.emp_no=s.emp_no
and d.to_date='9999-01-01'
and s.to_date='9999-01-01'
group by d.dept_no
select title,count(title) as t
from titles
group by title
having t>=2
select distinct title,count(distinct emp_no)as t
from titles
group by title
having t>=2
select *
from employees
where emp_no%2=1
and last_name<>'Mary'
order by hire_date desc;
select t.title,avg(s.salary) as avg
from titles as t,salaries as s
where s.to_date='9999-01-01'
and t.to_date='9999-01-01'
and s.emp_no=t.emp_no
group by t.title
select emp_no,max(salary)
from salaries
where to_date='9999-01-01'
and salary not in (select max(salary) from salaries)
或者
select emp_no,salary
from salaries
where to_date='9999-01-01'
order by salary desc limit 1,1
select s.emp_no,max(s.salary),e.last_name,e.first_name
from salaries as s,employees as e
where s.emp_no=e.emp_no
and s.to_date='9999-01-01'
and s.salary not in (select max(salary) from salaries)
select e.last_name,e.first_name,d.dept_name
from employees as e left join dept_emp as dp
on e.emp_no=dp.emp_no
left join departments as d
on d.dept_no=dp.dept_no
select
(select salary from salaries where emp_no=10001 order by to_date desc limit 1)-
(select salary from salaries where emp_no=10001 order by to_date limit 1)
as growth
select a.emp_no,(b.salary-c.salary)as growth
from employees as a
inner join
salaries as b
on a.emp_no=b.emp_no
and b.to_date='9999-01-01'
inner join
salaries as c
on c.emp_no=b.emp_no
and a.hire_date=c.from_date
order by growth
答案
select dp.dept_no,d.dept_name,count(s.salary)as sum
from dept_emp as dp,departments as d,salaries as s
where d.dept_no=dp.dept_no
and dp.emp_no=s.emp_no
group by d.dept_no
自己写的(属实垃圾)?
select d.dept_no,d.dept_name,new.sum
from departments as d
inner join
(select dp.dept_no,count(s.salary) as sum
from dept_emp as dp,salaries as s
where dp.emp_no=s.emp_no
group by dp.dept_no)as new
on d.dept_no=new.dept_no
自己写的,属实优秀??
select emp_no,salary,dense_rank() over (order by salary desc)
from salaries
where to_date='9999-01-01'
答案写的
select s1.emp_no,s1.salary,count(distinct s2.salary) rank
from salaries s1, salaries s2
where s1.salary <= s2.salary and s1.to_date = '9999-01-01' and s2.to_date = '9999-01-01'
group by s1.emp_no order by rank;
select dp.dept_no,dp.emp_no,s.salary
from dept_emp as dp,salaries as s
where dp.emp_no not in (select emp_no from dept_manager where to_date='9999-01-01')
and dp.emp_no = s.emp_no
and dp.to_date='9999-01-01'
and s.to_date='9999-01-01'
select a.emp_no,b.emp_no as manager_no,a.salary as emp_salary,b.salary as manager_salary
from
(select dp.emp_no,dp.dept_no,s.salary
from dept_emp as dp,salaries as s
where dp.emp_no=s.emp_no
and s.to_date='9999-01-01'
and dp.emp_no not in (select emp_no from dept_manager))as a,
(select dm.emp_no,dm.dept_no,s.salary
from dept_manager as dm,salaries as s
where dm.emp_no=s.emp_no
and s.to_date='9999-01-01')as b
where a.salary>b.salary
and a.dept_no=b.dept_no
本题思想,制作两张表,一个员工表,一个经理表
select dp.dept_no ,dm.dept_name,t.title,count(t.title)as count
from titles as t inner join dept_emp as dp
on t.emp_no=dp.emp_no
and t.to_date='9999-01-01'
and dp.to_date='9999-01-01'
inner join departments as dm
on dm.dept_no=dp.dept_no
group by dp.dept_no,t.title
自己写的
select d.dept_no,d.dept_name,t.title,count(t.emp_no)as count
from departments as d,dept_emp as dp,titles as t
where d.dept_no=dp.dept_no
and dp.emp_no=t.emp_no
and dp.to_date='9999-01-01'
and t.to_date='9999-01-01'
group by d.dept_no,t.title
select s2.emp_no,s2.from_date,(s2.salary-s1.salary)as salary_growth
from salaries as s1,salaries as s2
where s1.emp_no=s2.emp_no
and salary_growth>5000
and (strftime('%Y',s2.to_date)-strftime('%Y',s1.to_date)=1
or strftime('%Y',s2.from_date)-strftime('%Y',s1.from_date)=1)
order by salary_growth desc
select c.name,count(fc.film_id)
from (select category_id,count(film_id)as category_num from film_category group by category_id having count(film_id)>=5) as cc,
film as f,
category as c,
film_category as fc
where f.description like'%robot%'
and f.film_id=fc.film_id
and c.category_id=fc.category_id
and cc.category_id=c.category_id
select f.film_id,f.title
from film as f left join film_category as fc
on f.film_id=fc.film_id
where fc.category_id is null
select f.title,f.description
from film as f,(select fc.film_id from film_category as fc,category as c
where c.category_id=fc.category_id
and c.name='Action')as new
where f.film_id=new.film_id
这个是自己写的
select title,description
from film
where film.film_id in
(select fc.film_id from film_category as fc,category as c
where c.category_id=fc.category_id
and c.name='Action')
以下这两个都是网上的
select title,description
from film
where film.film_id in
(select film_id
from film_category
where category_id in (
select category_id
from category
where name='Action'))
select title,description
from film
where film.film_id in
(select film_id
from film_category
where category_id in (
select category_id
from category
where name='Action'))
这是sqlite的方法
select last_name||" "|| first_name
from employees
这是mysql的方法
select concat(last_name,' ',first_name)
from employees
create table actor(
actor_id smallint(5) not null primary key,
first_name varchar(45) not null,
last_name varchar(45) not null,
last update timestamp not null default(datetime('now','localtime'))
);
INSERT or ignore INTO ACTOR
(actor_id,first_name,last_name,last_update)
values(
3,'ED','CHASE','2006-02-15 12:34:33'
)
create table actor_name
(
first_name varchar(45) not null,
last_name varchar(45) not null
);
insert into actor_name
select first_name,last_name
from actor;
创建唯一索引
create unique index 索引名 on 表名(某列名)
创建普通索引
create index 索引名 on 表名(某列名)
create unique index uniq_idx_firstname on actor(first_name);
create index idx_lastname on actor(last_name);
我觉得视图就像是一个虚拟新表,后来执行查询时就可以在这个虚拟新表中进行。
create view 视图名 as
select ...
from...
...
create view actor_name_view as
select first_name as first_name_v,last_name as last_name_v
from actor;
mysql
select|delete|update column1,column2,...
forced index index_name
where (condition);
sqlite
select|delete|update column1,column2,...
from table_name
indexed by index_name
where (condition);
alter table table_name
add column column_name type ...,
add column column_name type ...;
alter table actor
add column create_date datetime not null default('0000 00:00:00')
create trigger audit_log after insert on employees
begin
insert into audit values(new.id,new.name);
end;
delete from table_name
where (condition)
delete from titles_test
where id not in (select min(id) from titles_test group by emp_no);
update table_name
set
更新要求1,
更新要求2,
...
where(condition)
update titles_test
set
to_date=NULL,
from_date='2001-01-01'
where to_date='9999-01-01';
update table_name
set column_name=replace(column_name,str1,str2)
where(condition)
update titles_test
set emp_no=replace(emp_no,10001,10005)
where id =5;
alter table table_name
rename [to|as] new_table_name;
或者(但是下面这种在牛客网上不通过,在mysql上可以通过)
rename table table_name to new_table_name;
alter table titles_test
rename titles_2017;
update salaries
set salary=salary*1.1
where emp_no in (select emp_no from emp_bonus)
and to_date='9999-01-01'
这题答案是用sqlite写的
select "select count(*) from "||name|| ';'as cnts
from sqlite_master where type='table';
1.在 SQLite 系统表 sqlite_master 中可以获得所有表的索引,其中字段 name 是所有表的名字,而且对于自己创建的表而言,字段 type 永远是 ‘table’,详情可参考:博客
2.在 SQLite 中用 “||” 符号连接字符串
mysql写法:
select concat("select count(*) from",' ',table_name,";")as cnts
from (select table_name from information_schema.tables) as new;
这里要注意必须给from后面的这个表加一个别名,不然会出现every derived table must has its own alias的错误。
将employees表中的所有员工的last_name和first_name通过(‘)连接起来。
myql写法:
select concat(last_name,"'",first_name)as name
from employees;
sqlite写法:
select last_name||"'"||first_name as name
from employees;
1个汉字=1个字=1个字符
一个字母=一个字符=1个字节
1个字符=1个字节=8bit(ASCII码)
1个字符=2个字节=16bit(Unicode码下)
1个字符=3个字节(utf-8)
length()计算的是字节长度
char_length()计算的是字符长度
查找字符串’10,A,B‘中逗号’,'出现的次数cnt.
select lenth('10,A,B')-length(replace('10,A,B',',','')) as cnt;
substr(str,pos),从postion位置开始的往后的所有字符
substr(str from pos),同上
substr(str,pos,len),从position位置开始的往后的len个字符
select first_name
from employees
order by substr(first_name,-2);
或者:
select first_name
from employees
order by substr(first_name,-2,2);
官方文档
例子:
按班级进行汇总,把同一班级的用逗号隔开。group_concat()默认是用逗号隔开的
牛客网题目:按照dept_no汇总,属于同一个部门的emp_no按照逗号进行连接,结果给出dept_no以及连接出的结果employees.
select dept_no,group_concat(emp_no)as employees
from dept_emp
group by dept_no;
select avg(salary) as avg_salary
from salaries
where salary not in (select max(salary) from salaries)
and salary not in (select min(salary) from salaries)
and to_date='9999-01-01';
select column_1,column_2 from table_name limit 偏移量,查询条数;
分页查询employees,每5行一页,返回第2页的数据
select * from employees limit 5,5;
偏移量从0计数 ,每5行一页,那么第2页的数据是6-10行,所以limit5,5是从第6行起查询5条数据
select e.emp_no,dp.dept_no,eb.btype,eb.recevied
from employees as e inner join dept_emp as dp
on e.emp_no=dp.emp_no
left join emp_bonus as eb
on dp.emp_no=eb.emp_no;
select * from employees
where not exists (select emp_no from dept_emp
where dept_emp.emp_no=employees.emp_no);
给出emp_no、first_name、last_name、奖金类型btype、对应的当前薪水情况salary以及奖金金额bonus。 bonus类型btype为1其奖金为薪水salary的10%,btype为2其奖金为薪水的20%,其他类型均为薪水的30%。 当前薪水表示to_date=‘9999-01-01’。
考察case when用法。
case column_name
when column_name满足某条件 then
when···
else ···
end
答案
select eb.emp_no,e.first_name,e.last_name,eb.btype,s.salary,
(case eb.btype
when 1 then s.salary*0.1
when 2 then s.salary*0.2
else s.salary*0.3
end)as bonus
from emp_bonus as eb,employees as e,salaries as s
where eb.emp_no=e.emp_no
and eb.emp_no=s.emp_no
and s.to_date='9999-01-01';
我自己写的
select bb.emp_no,e.first_name,e.last_name,bb.btype,bb.salary,bb.bonus
from (select eb.emp_no,eb.btype,s.salary,
(case eb.btype
when 1 then s.salary*0.1
when 2 then s.salary*0.2
else s.salary*0.3
end
)as bonus
from emp_bonus as eb inner join salaries as s
on eb.emp_no = s.emp_no
and s.to_date='9999-01-01') as bb
inner join employees as e
on bb.emp_no=e.emp_no;
select a.emp_no,a.salary,sum(b.salary) as running_total
from salaries as a,salaries as b
where a.emp_no>=b.emp_no
and a.to_date='9999-01-01'
and b.to_date='9999-01-01'
group by a.emp_no;
select first_name
from employees as a
where (select count(*) from employees as b
where b.first_name>=a.first_name)%2=1;
自己写的(牛客网上不通过,但是mysql上可以通过)
select first_name
from employees,(select @rownum:=0)as r
where (@rownum:=@rownum+1)%2=1;