倒数第三个员工信息
SELECT * FROM employees ORDER BY hire_date DESC LIMIT 2,1;
LIMIT m,n : 表示从第m+1条开始,取n条数据;
LIMIT n : 表示从第0条开始,取n条数据,是limit(0,n)的缩写
薪资涨15次以上的
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 emp_no FROM employees
WHERE emp_no NOT IN (SELECT emp_no FROM dept_manager)
title进行分组,显示大于2的组
SELECT title, COUNT(title) AS t FROM titles
GROUP BY title HAVING t >= 2
同上,忽略重复的emp_no
SELECT title, COUNT(DISTINCT emp_no) AS t FROM titles
GROUP BY title HAVING t >= 2
当前各个title类对应的员工平均薪资
SELECT title , AVG(salary) AS avg
FROM salaries s,titles t
WHERE s.emp_no = t.emp_no AND s.to_date = '9999-01-01' AND t.to_date = '9999-01-01'
GROUP BY t.title;
查找员工编号emp_now为10001其自入职以来的薪水salary涨幅值growth
SELECT (MAX(salary)-MIN(salary)) AS growth
FROM salaries WHERE emp_no = '10001'
查找所有员工自入职以来的薪水涨幅情况,给出员工编号emp_noy以及其对应的薪水涨幅growth,并按照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 a.emp_no = c.emp_no and a.hire_date = c.from_date
order by growth asc
统计各个部门对应员工涨幅的次数总和,给出部门编码dept_no、部门名称dept_name以及次数sum
SELECT de.dept_no, dp.dept_name, COUNT(s.salary) AS sum
FROM (dept_emp AS de INNER JOIN salaries AS s ON de.emp_no = s.emp_no)
INNER JOIN departments AS dp ON de.dept_no = dp.dept_no
GROUP BY de.dept_no
对所有员工的当前(to_date='9999-01-01')薪水按照salary进行按照1-N的排名,相同salary并列且按照emp_no升序排列
SELECT s1.emp_no, s1.salary, COUNT(DISTINCT s2.salary) AS rank
FROM salaries AS s1, salaries AS s2
WHERE s1.to_date = '9999-01-01' AND s2.to_date = '9999-01-01' AND s1.salary <= s2.salary
GROUP BY s1.emp_no
ORDER BY s1.salary DESC, s1.emp_no ASC
获取员工其当前的薪水比其manager当前薪水还高的相关信息,
SELECT sem.emp_no AS emp_no, sdm.emp_no AS manager_no, sem.salary AS emp_salary, sdm.salary AS manager_salary
FROM (SELECT s.salary, s.emp_no, de.dept_no FROM salaries s INNER JOIN dept_emp de
ON s.emp_no = de.emp_no AND s.to_date = '9999-01-01' ) AS sem,
(SELECT s.salary, s.emp_no, dm.dept_no FROM salaries s INNER JOIN dept_manager dm
ON s.emp_no = dm.emp_no AND s.to_date = '9999-01-01' ) AS sdm
WHERE sem.dept_no = sdm.dept_no AND sem.salary > sdm.salary
汇总各个部门当前员工的title类型的分配数目
SELECT de.dept_no, dp.dept_name, t.title, COUNT(t.title) AS count
FROM titles AS t INNER JOIN dept_emp AS de
ON t.emp_no = de.emp_no AND de.to_date = '9999-01-01' AND t.to_date = '9999-01-01'
INNER JOIN departments AS dp
ON de.dept_no = dp.dept_no
GROUP BY de.dept_no, t.title
给出每个员工每年薪水涨幅超过5000的员工
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 * from employees对应的执行计划
EXPLAIN SELECT * FROM employees
将employees表的所有员工的last_name和first_name拼接起来作为Name,中间以一个空格区分
SELECT last_name||" "||first_name AS 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')) -- ,
-- PRIMARY KEY(actor_id)
)
插入数据
INSERT INTO actor
VALUES (1, 'PENELOPE', 'GUINESS', '2006-02-15 12:34:33'),
(2, 'NICK', 'WAHLBERG', '2006-02-15 12:34:33')
对于表actor批量插入如下数据,如果数据已经存在,请忽略,不使用replace操作
INSERT OR IGNORE INTO actor VALUES (3, 'ED', 'CHASE', '2006-02-15 12:34:33')
INSERT OR REPLACE INTO actor VALUES (3, 'ED', 'CHASE', '2006-02-15 12:34:33')
创建新表导入数据
INSERT INTO actor_name SELECT first_name,last_name FROM actor;
对first_name创建唯一索引uniq_idx_firstname,对last_name创建普通索引idx_lastname
CREATE UNIQUE INDEX uniq_idx_firstname ON actor(first_name);
CREATE INDEX idx_lastname ON actor(last_name);
针对actor表创建视图actor_name_view,只包含first_name以及last_name两列,并对这两列重新命名,fist_name为first_name_v,last_name修改为last_name_v:
CREATE VIEW actor_name_view AS
SELECT first_name AS fist_name_v, last_name AS last_name_v
FROM actor
针对salaries表emp_no字段创建索引idx_emp_no,查询emp_no为10005, 使用强制索引
SELECT * FROM salaries INDEXED BY idx_emp_no WHERE emp_no = 10005
新增加一列
ALTER TABLE actor ADD COLUMN create_date datetime
构造一个触发器audit_log,在向employees表中插入一条数据的时候,触发插入相关的数据到audit中
CREATE TRIGGER audit_log AFTER INSERT ON employees_test
BEGIN
INSERT INTO audit VALUES(NEW.ID,NEW.NAME);
END;
删除emp_no重复的记录,只保留最小的id对应的记录
DELETE FROM titles_test WHERE id NOT IN
(SELECT MIN(id) FROM titles_test GROUP BY emp_no)
将所有to_date为9999-01-01的全部更新为NULL,且 from_date更新为2001-01-01
update titles_test set to_date=null ,from_date ='2001-01-01'
where to_date='9999-01-01'
将titles_test表名修改为titles_2017
ALTER TABLE titles_test RENAME TO titles_2017
在audit表上创建外键约束,其emp_no对应employees_test表的主键id
DROP TABLE audit;
CREATE TABLE audit(
EMP_no INT NOT NULL,
create_date datetime NOT NULL,
FOREIGN KEY(EMP_no) REFERENCES employees_test(ID));
将所有获取奖金的员工当前的薪水增加10%
UPDATE salaries SET salary = salary * 1.1 WHERE emp_no IN
(SELECT s.emp_no FROM salaries AS s INNER JOIN emp_bonus AS eb
ON s.emp_no = eb.emp_no AND s.to_date = '9999-01-01')
针对库中的所有表生成select count(*)对应的SQL语句
SELECT "select count(*) from " || name || ";" AS cnts
FROM sqlite_master WHERE type = 'table'
查找字符串'10,A,B' 中逗号','出现的次数cnt。
SELECT (length("10,A,B")-length(replace("10,A,B",",","")))/length(",") AS cnt
获取Employees中的first_name,查询按照first_name最后两个字母,按照升序进行排列
SELECT first_name FROM employees ORDER BY substr(first_name,length(first_name)-1)
按照dept_no进行汇总,属于同一个部门的emp_no按照逗号进行连接
SELECT dept_no, group_concat(emp_no) AS employees
FROM dept_emp GROUP BY dept_no
查找排除当前最大、最小salary之后的员工的平均工资avg_salary
ELECT AVG(salary) AS avg_salary FROM salaries
WHERE to_date = '9999-01-01'
AND salary NOT IN (SELECT MAX(salary) FROM salaries)
AND salary NOT IN (SELECT MIN(salary) FROM salaries)
使用含有关键字exists查找未分配具体部门的员工的所有信息
exists关键字:强调的是是否返回结果集,不要求知道返回什么。只要返回了字段,就是真
select * from employees ee where not exists (
select emp_no from dept_emp de where de.emp_no = ee.emp_no
)
奖金运算
SELECT e.emp_no, e.first_name, e.last_name, b.btype, s.salary,
(CASE b.btype
WHEN 1 THEN s.salary * 0.1
WHEN 2 THEN s.salary * 0.2
ELSE s.salary * 0.3 END) AS bonus
FROM employees AS e INNER JOIN emp_bonus AS b ON e.emp_no = b.emp_no
INNER JOIN salaries AS s ON e.emp_no = s.emp_no AND s.to_date = '9999-01-01'
按照salary的累计和running_total,其中running_total为前所有员工的salary累计和,其他以此类推。 具体结果如下Demo展示
SELECT s1.emp_no, s1.salary,
(SELECT SUM(s2.salary) FROM salaries AS s2
WHERE s2.emp_no <= s1.emp_no AND s2.to_date = '9999-01-01') AS running_total
FROM salaries AS s1 WHERE s1.to_date = '9999-01-01'
对于employees表中,给出奇数行的first_name
SELECT e1.first_name FROM
(SELECT e2.first_name,
(SELECT COUNT(*) FROM employees AS e3
WHERE e3.first_name <= e2.first_name)
AS rowid FROM employees AS e2) AS e1
WHERE e1.rowid % 2 = 1
来源:CSDN
作者:香草冰冰
链接:https://blog.csdn.net/u011624267/article/details/103793283