涉及的数据库:链接:https://pan.baidu.com/s/17p2QxOt-wlLuTciQchm6Qw 提取码:1234
1.运算符
SELECT 100+'a';
SELECT 100+NULL;
SELECT
`last_name`,
`salary`
FROM
`employees`
WHERE
`salary` NOT BETWEEN 5000 AND 12000;
SELECT
`last_name`,
`department_id`
FROM
`employees`
WHERE
`department_id`=20 OR `department_id`=50;
SELECT
`last_name`,
`job_id`
FROM
`employees`
WHERE
`manager_id` IS NULL;
SELECT
`last_name`,
`salary`
FROM
`employees`
WHERE
`commission_pct` IS NOT NULL;
SELECT
`last_name`
FROM
`employees`
WHERE
`last_name` LIKE '__a%';
SELECT
`last_name`
FROM
`employees`
WHERE
`last_name`LIKE '%a%' OR
`last_name`LIKE '%k%';
SELECT
*
FROM
`employees`
WHERE
`first_name`LIKE'%e';
SELECT
`last_name`,
`job_id`
FROM
`employees`
WHERE
`department_id` BETWEEN 80 AND 100;
SELECT
`last_name`,
`salary`,
`manager_id`
FROM
`manager_id` IN (100,101,110);
2.排序和分页
SELECT
`last_name`,
`department_id`,
salary*(1+IFNULL(`commission_pct`,0)) AS sumsalary
FROM
`employees`
ORDER BY
sumsalary DESC,
`last_name`
SELECT
`last_name`,
`salary`
FROM
`employees`
WHERE
`salary` NOT BETWEEN 8000 AND 17000
ORDER BY
`salary` DESC
LIMIT
20,20;
SELECT
*
FROM
`employees`
WHERE
`email`LIKE'%e%'
ORDER BY
LENGTH(`email`) DESC,
`department_id` ;
3.多表查询
SELECT
e.`last_name`,
e.`department_id`,
d.`department_name`
FROM
`employees` e
LEFT OUTER JOIN
`departments` d
ON
e.`department_id`=d.`department_id`;
SELECT
e.`job_id`,
d.`location_id`
FROM
`departments` d
INNER JOIN
`employees` e
ON
d.`department_id`=e.`department_id`
WHERE
d.`department_id`=90;
SELECT
e.`last_name`,
e.`department_id`,
l.`location_id`,
l.`city`
FROM
`departments` d
INNER JOIN
`locations` l
ON
d.`location_id`=l.`location_id`
RIGHT JOIN
`employees` e
ON
d.`department_id`=e.`department_id`
WHERE
e.`commission_pct`IS NOT NULL;
SELECT
COUNT(*)
FROM
`employees`
WHERE
`commission_pct` IS NOT NULL;
SELECT
e.`last_name`,
e.`job_id`,
e.`department_id`,
d.`department_name`
FROM
`departments` d
INNER JOIN
`locations` l
ON
d.`location_id`=l.`location_id`
INNER JOIN
`employees` e
ON
d.`department_id`=e.`department_id`
WHERE
l.`city`='Toronto';
SELECT
d.`department_name`,
l.`street_address`,
e.`last_name`,
e.`job_id`,
e.`salary`
FROM
`employees` e
RIGHT JOIN
`departments` d
ON
d.`department_id`=e.`department_id`
INNER JOIN
`locations` l
ON
d.`location_id`=l.`location_id`
WHERE
d.`department_name`='Executive';
SELECT
e.`last_name` AS '员工id',
e.`employee_id`,
b.`last_name` AS '老板id',
b.`employee_id`
FROM
`employees` e
LEFT JOIN
`employees` b
ON
e.`manager_id`=b.`employee_id`;
SELECT
d.`department_id`
FROM
`departments` d
LEFT JOIN
`employees` e
ON
d.`department_id`=e.`department_id`
WHERE
e.`department_id` IS NULL;
SELECT
l.`city`
FROM
`locations` l
LEFT JOIN
`departments` d
ON
l.`location_id`=d.`location_id`
WHERE
d.`location_id`IS NULL;
SELECT
e.*
FROM
`employees` e
JOIN
`departments` d
ON
e.`department_id`=d.`department_id`
WHERE
`department_name`IN('Sales','IT');
4.单行函数
SELECT NOW();
SELECT
`employee_id`,
`salary`,
`salary`*(1+0.2) AS newsalary
FROM
`employees`;
SELECT
`last_name`,
LENGTH(`last_name`)
FROM
`employees`
ORDER BY
SUBSTR(`last_name`,1,1) ASC;
SELECT
CONCAT(`employee_id`,`last_name`,`salary`) AS OUT_PUT
FROM
`employees`;
SELECT
`employee_id`,
DATEDIFF(CURDATE(),`hire_date`) AS workday,
DATEDIFF(CURDATE(),`hire_date`)/365 AS workyear
FROM
`employees`
ORDER BY
workyear DESC;
SELECT
`last_name`,
`hire_date`,
`manager_id`
FROM
`employees`
WHERE
DATE_FORMAT(`hire_date`,'%Y-%m-%d')>='1997-01-01'
AND
`department_id`IN(80,90,100)
AND
`commission_pct` IS NOT NULL;
SELECT
`last_name`,
`hire_date`
FROM
`employees`
WHERE
DATEDIFF(CURDATE(),`hire_date`)>=10000;
5. 聚合函数
SELECT
MAX(salary),
MIN(salary),
AVG(salary),
SUM(salary)
FROM
`employees`
SELECT
MAX(salary),
MIN(salary),
AVG(salary),
SUM(salary)
FROM
`employees`
GROUP BY
`job_id`
SELECT
`job_id`,
COUNT(*)
FROM
`employees`
GROUP BY
`job_id`;
SELECT
MAX(salary)-MIN(salary)
FROM
`employees`;
SELECT
MIN(salary)
FROM
`employees`
WHERE
`manager_id` IS NOT NULL
GROUP BY
`manager_id`
HAVING
MIN(salary)>=6000;
SELECT
`department_name`,
d.`location_id`,
COUNT(*),
AVG(`salary`) AS avgsalary
FROM
`employees` e
RIGHT JOIN
`departments` d
ON
e.`department_id`=d.`department_id`
GROUP BY
d.`department_id`
ORDER BY
avgsalary DESC;
SELECT
`job_id`,
`department_name`,
MIN(salary)
FROM
`departments` d
LEFT JOIN
`employees` e
ON
d.`department_id`=e.`department_id`
GROUP BY
`department_name`,
`job_id`
6.子查询
SELECT
`last_name`,
`salary`
FROM
`employees`
WHERE
`department_id`=
(
SELECT
`department_id`
FROM
`employees`
WHERE
`last_name`='Zlotkey'
);
SELECT
`employee_id`,
`last_name`,
`salary`
FROM
`employees`
WHERE
`salary`>=(
SELECT
AVG(salary)
FROM
`employees`
);
SELECT
`last_name`,
`job_id`,
`salary`
FROM
`employees`
WHERE
`salary`>ALL(
SELECT
`salary`
FROM
`employees`
WHERE
`job_id`='SA_MAN'
);
SELECT
`employee_id`,
`last_name`
FROM
`employees`
WHERE
`department_id`IN(
SELECT
DISTINCT `department_id`
FROM
`employees`
WHERE
`last_name` LIKE'%u%'
);
SELECT
`employee_id`
FROM
`employees` e
WHERE
e.`department_id` IN(
SELECT
d.`department_id`
FROM
`departments` d
WHERE
d.`location_id`='1700'
);
SELECT
e.`last_name`,
e.`salary`
FROM
`employees` e
WHERE
e.`manager_id`IN
(
SELECT
`employee_id`
FROM
`employees`
WHERE
`last_name`='King'
);
SELECT
`last_name`,
`salary`
FROM
`employees`
WHERE
`salary`=
(
SELECT
MIN(salary)
FROM
`employees`
);
SELECT
*
FROM
`departments` d
WHERE
d.`department_id`=(
SELECT
e.`department_id`
FROM
`employees` e
GROUP BY
e.`department_id`
ORDER BY
AVG(salary) ASC
LIMIT 1
)
SELECT
*
FROM
`departments`
WHERE
`department_id`=(
SELECT
`department_id`
FROM
`employees`
GROUP BY
`department_id`
HAVING
AVG(salary)=(
SELECT
MIN(avg_sal)
FROM (SELECT
AVG(salary) avg_sal
FROM
`employees`
GROUP BY
`department_id`)avgsalary
)
)
SELECT
d.*,
(
SELECT
AVG(salary)
FROM
`employees`
GROUP BY
`department_id`
HAVING
`department_id`=d.`department_id`
) min_avg
FROM
`departments` d
WHERE
d.`department_id`=(
SELECT
`department_id`
FROM
`employees`
GROUP BY
`department_id`
HAVING
AVG(salary)=(
SELECT
MIN(avg_sal)
FROM (SELECT
AVG(salary) avg_sal
FROM
`employees`
GROUP BY
`department_id`)avgsalary
)
)
SELECT
d.*,
(
SELECT
AVG(`salary`)
FROM
`employees`
GROUP BY
`department_id`
HAVING
`department_id`=d.`department_id`
)min_avg
FROM
`departments` d
WHERE
d.`department_id`=(
SELECT
e.`department_id`
FROM
`employees` e
GROUP BY
e.`department_id`
ORDER BY
AVG(salary) ASC
LIMIT 1
)
SELECT
*
FROM
`jobs` j
WHERE
j.`job_id`=(
SELECT
e.`job_id`
FROM
`employees` e
GROUP BY
e.`job_id`
ORDER BY
AVG(salary) DESC
LIMIT 1
);
SELECT
`department_id`
FROM
`employees`
WHERE
`department_id` IS NOT NULL
GROUP BY
`department_id`
HAVING AVG(salary)>(
SELECT
AVG(salary)
FROM
`employees`
);
SELECT
e1.`employee_id`,
e1.`first_name`,
e1.`last_name`
FROM
`employees` e1
WHERE EXISTS(
SELECT
*
FROM
`employees` e2
WHERE
e1.`employee_id`=e2.`manager_id`
)
SELECT
MIN(salary)
FROM
(
SELECT
e1.`salary`
FROM
`employees` e1
WHERE
e1.`department_id`=(
SELECT
e2.`department_id`
FROM
`employees` e2
GROUP BY
e2.`department_id`
ORDER BY
MAX(e2.salary) ASC
LIMIT 1
)
)e
SELECT
MIN(salary)
FROM
`employees`
WHERE
`department_id`=(
SELECT
`department_id`
FROM
`employees`
GROUP BY
`department_id`
HAVING
MAX(salary)=(
SELECT
MIN(max_sal)
FROM (
SELECT
MAX(salary) max_sal
FROM
`employees`
GROUP BY
`department_id`
)t_dept_max_sala
)
);
SELECT
DISTINCT b.`last_name`,
b.`department_id`,
b.`salary`
FROM
`employees` b
JOIN
`employees` e
ON
b.`employee_id`=e.`manager_id`
AND
e.`department_id`=
(
SELECT
e1.`department_id`
FROM
`employees` e1
GROUP BY
e1.`department_id`
HAVING
AVG(e1.salary)=(
SELECT
AVG(e2.salary)
FROM
`employees` e2
GROUP BY
e2.`department_id`
ORDER BY
AVG(e2.salary) DESC
LIMIT 1
)
)
SELECT
DISTINCT `last_name`,
`department_id`,
`salary`
FROM
`employees`
WHERE
`employee_id`IN(
SELECT
DISTINCT `manager_id`
FROM
`employees`
WHERE
`department_id`=(
SELECT
`department_id`
FROM
`employees`
GROUP BY
`department_id`
HAVING
AVG(salary)=(
SELECT
MAX(avg_sal)
FROM(
SELECT
AVG(salary) avg_sal
FROM
`employees`
GROUP BY
`department_id`
)t_dept_avg_sal
)
)
)
SELECT
DISTINCT`department_id`
FROM
`employees`
WHERE
`department_id` NOT IN(
SELECT
DISTINCT`department_id`
FROM
`employees`
WHERE
`job_id`=('ST_CLERK')
)
SELECT
e1.`last_name`
FROM
`employees` e1
WHERE NOT EXISTS(
SELECT
*
FROM
`employees` e2
WHERE
e1.`manager_id`=e2.`employee_id`
);
SELECT
e.`employee_id`,
e.`last_name`,
e.`hire_date`,
e.`salary`
FROM
`employees` e
WHERE
e.`manager_id`IN(
SELECT
b.`employee_id`
FROM
`employees` b
WHERE
b.`last_name`='De Haan'
);
SELECT
e.`employee_id`,
e.`last_name`,
e.`hire_date`,
e.`salary`
FROM
`employees` e
WHERE EXISTS(
SELECT
*
FROM
`employees` b
WHERE
e.`manager_id`=b.`employee_id`
AND
b.`last_name`='De Haan'
);
SELECT
e1.`employee_id`,
e1.`last_name`,
e1.`salary`
FROM
`employees` e1
WHERE
e1.salary>(
SELECT
AVG(salary)
FROM
`employees` e2
GROUP BY
e2.`department_id`
HAVING
e1.`department_id`=e2.`department_id`
)
SELECT
d.`department_name`
FROM
`departments` d
WHERE
5<(
SELECT
COUNT(*)
FROM
`employees` e
WHERE
e.`department_id`=d.`department_id`
)
SELECT
l.`country_id`
FROM
`locations` l
WHERE
2<(
SELECT
COUNT(*)
FROM
`departments` d
WHERE
d.`location_id`=l.`location_id`
)
|