select * from 'employees' where hire_date in (select MAX(hire_date) from 'employees');
select * from(select * from 'employees' order by hire_date desc) limit 1 offset 2;
select a.emp_no, a.salary, a.from_date, a.to_date, d.dept_no from
(select * from 'salaries' where to_date='9999-01-01') a, 'dept_manager' d
where a.emp_no=d.emp_no and d.to_date='9999-01-01';
select last_name,first_name,dept_no from dept_emp d,employees e
where e.emp_no = d.emp_no;
select last_name,first_name,dept_no from employees as e left outer join
dept_emp as d on e.emp_no = d.emp_no;
select e.emp_no,salary from employees e,salaries s
where e.emp_no = s.emp_no and hire_date = from_date
order by e.emp_no desc;
select emp_no,count(*) 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 dept_no, d.emp_no, salary from
dept_manager d, salaries s
where d.to_date='9999-01-01' and d.emp_no = s.emp_no
and d.to_date = s.to_date;
select e.emp_no from employees e where e.emp_no not in
(select emp_no from dept_manager);
select emp_no,manager_no from dept_emp de,
(select dept_no,emp_no as manager_no from dept_manager where to_date='9999-01-01') dm
where de.dept_no = dm.dept_no and emp_no <> manager_no and to_date='9999-01-01';
select dept_no,de.emp_no,MAX(salary)
from dept_emp de, salaries s
where de.emp_no = s.emp_no and de.to_date='9999-01-01'
and de.to_date = s.to_date
group by dept_no;
select title,count(emp_no) as t
from titles group by title
having t >= 2;
select title, count(distinct emp_no) t
from titles group by title
having t >= 2;
select * from employees where last_name <> 'Mary'
and round(emp_no/2.0) <> emp_no/2
order by hire_date desc;
select title,AVG(salary) avg from salaries s,titles t
where s.emp_no = t.emp_no and s.to_date = '9999-01-01' and s.to_date = t.to_date
group by title;
select emp_no,salary from salaries
where to_date='9999-01-01'
order by salary desc limit 1 offset 1;
select e.emp_no, salary, last_name, first_name
from employees e, salaries s
where e.emp_no = s.emp_no and salary in
(select MAX(salary) from salaries where salary not in
(select MAX(salary) from salaries where to_date='9999-01-01')
and to_date='9999-01-01');
select last_name,first_name,dd.dept_name from employees as e left outer
join
(select emp_no,d.dept_no,dept_name from departments d,dept_emp de
where d.dept_no = de.dept_no) as dd
on e.emp_no=dd.emp_no;
select MAX(salary)-MIN(salary) as growth
from salaries
where emp_no = 10001;
select e.emp_no,(s1.salary-s2.salary) as growth
from employees as e
inner join salaries as s1
on e.emp_no = s1.emp_no and s1.to_date = '9999-01-01'
inner join salaries as s2
on e.emp_no = s2.emp_no and hire_date = s2.from_date
order by growth asc;
select d.dept_no,dept_name,count(de.emp_no) as sum
from departments d, dept_emp de, salaries s
where d.dept_no = de.dept_no and de.emp_no = s.emp_no
group by de.dept_no;
select s1.emp_no,s1.salary,count(distinct s2.salary) as rank
from salaries s1, salaries s2
where s1.to_date='9999-01-01'
and s1.to_date = s2.to_date
and s1.salary <= s2.salary
group by s1.emp_no
order by s1.salary desc,s1.emp_no asc;
select de.dept_no,e.emp_no,salary
from dept_emp de, dept_manager dm, employees e, salaries s
where e.emp_no not in (select emp_no from dept_manager)
and e.emp_no = de.emp_no and e.emp_no = s.emp_no
and de.dept_no = dm.dept_no
and de.to_date = '9999-01-01'
and de.to_date = s.to_date;
select m2.eno,manager_no,emp_salary,manager_salary from
(select dm.dept_no as dno,dm.emp_no as manager_no, s1.salary as manager_salary
from dept_manager dm, salaries s1
where dm.emp_no = s1.emp_no
and dm.to_date='9999-01-01'
and dm.to_date=s1.to_date) m1,
(select de.emp_no as eno, de.dept_no as dno, s2.salary as emp_salary
from dept_emp de, salaries s2
where de.emp_no = s2.emp_no
and de.to_date = '9999-01-01'
and de.to_date = s2.to_date) m2
where m2.dno = m1.dno
and emp_salary > manager_salary;
select d.dept_no,dept_name,title,count from departments d,
(select t.emp_no,de.dept_no as dno,title,count(title) as count
from dept_emp de, titles t
where de.emp_no = t.emp_no and de.to_date='9999-01-01' and de.to_date=t.to_date
group by de.dept_no,title) t1
where d.dept_no = t1.dno;
select s1.emp_no,s1.from_date,(s1.salary-s2.salary) as salary_growth
from salaries as s1 inner join salaries as s2
on s1.emp_no = s2.emp_no and
(strftime('%Y',s1.to_date)-strftime('%Y',s2.to_date)=1)
and salary_growth > 5000
order by salary_growth desc;
select name,count(fc.film_id) as amount from
(select category_id,count(film_id) as amount
from film_category
group by category_id
having amount >= 5) m1,film f, category c,film_category fc
where description like '%robot%'
and f.film_id= fc.film_id
and c.category_id = m1.category_id
and fc.category_id = c.category_id;
select f.film_id,title
from film as f left outer join film_category as fc
on f.film_id=fc.film_id
where category_id is null;
select title,description
from film
where film_id in
(select film_id from film_category fc
where category_id in(
select category_id from category where name='Action'))
explain select * from employees;
select last_name||' '||first_name as Name from employees;
create table actor(
actor_id smallint(5) primary key not null,
first_name varchar(45) not null,
last_name varchar(45) not null,
last_update timestamp default(datetime('now','localtime')) not null
);
insert into actor values
(1,'PENELOPE','GUINESS','2006-02-15 12:34:33'),
(2,'NICK','WAHLBERG','2006-02-15 12:34:33');
insert or ignore into actor 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 uniq_idx_firstname on actor(first_name);
create index idx_lastname on actor(last_name);
create view actor_name_view as
select first_name as first_name_v, last_name as last_name_v from actor;
select * from salaries
indexed by idx_emp_no
where emp_no=10005;
alter table actor
add create_date datetime default('0000-00-00 00:00:00') not null;
create trigger audit_log
after insert on employees_test
begin insert into audit
values (new.ID,new.NAME);
end
delete from titles_test
where id not in
(select MIN(id) from titles_test group by emp_no);
update titles_test
set to_date=null,from_date='2001-01-01'
where to_date='9999-01-01';
update titles_test
set emp_no=replace(emp_no,10001,10005)
where id=5;
alter table titles_test rename to titles_2017;
drop table audit;
create table audit(
EMP_no INT NOT NULL,
create_date datetime NOT NULL,
foreign key(EMP_no) references employees_test(ID))
select e1.* from emp_v e1,employees e2
where e1.emp_no=e2.emp_no;
update salaries
set salary = salary*1.1 where to_date='9999-01-01';
select "select count(*) from "||name||";" as cnts
from sqlite_master where type='table';
select last_name||"'"||first_name as name
from employees;
select (length("10,A,B")-length(replace("10,A,B",',',"")))/length(',')
as cnt;
select first_name from employees
order by substr(first_name,-2);
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 * from employees limit 5,5;
select de.emp_no,de.
dept_no,btype,recevied
from dept_emp as de outer left join emp_bonus as eb
on de.emp_no=eb.emp_no;
select * from employees e
where not exists (
select emp_no from dept_emp
where emp_no=e.emp_no);
select e.* from employees e,emp_v ev
where e.emp_no=ev.emp_no;
select e.emp_no,first_name,last_name,btype,salary,(
case btype
when 1 then salary*0.1
when 2 then salary*0.2
else salary*0.3
end) as bonus
from employees e,emp_bonus eb,salaries s
where e.emp_no=eb.emp_no and s.to_date='9999-01-01'
and e.emp_no=s.emp_no;
select emp_no,salary,(
select SUM(s2.salary) from salaries s2
where s1.emp_no >= s2.emp_no and s2.to_date='9999-01-01') as running_total
from salaries s1
where s1.to_date='9999-01-01';
select first_name
from employees e1
where (select count(*) from employees e2
where e2.first_name<=e1.first_name)%2 <> 0;
以上是我之前在牛客网做的一些SQL练习,如果发现有的语句存在问题或者有好的写法欢迎留言评论。