#查询t_employee表数据 ,*代表查询所有数据
SELECT * from t_employees;
#使用具体的列名称查询
SELECT EMPLOYEE_ID ,FIRST_NAME,EMAIL,SALARY from t_employees
SELECT EMPLOYEE_ID as ‘员工编号’ ,FIRST_NAME as ‘名’,EMAIL as ‘邮箱’,SALARY as ‘月薪资’ from t_employees
SELECT EMPLOYEE_ID,FIRST_NAME,SALARY*12 from t_employees
SELECT EMPLOYEE_ID,FIRST_NAME,SALARY%12 as ‘年薪’ from t_employees
select 1+2;
#去重的方法 DISTINCT
select DISTINCT(DEPARTMENT_ID) from t_employees;
SELECT EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees ORDER BY FIRST_NAME ASC ;
SELECT EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees ORDER BY SALARY DESC ;
SELECT EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees ORDER BY SALARY DESC ,EMPLOYEE_ID DESC ;
SELECT MANAGER_ID from t_employees order by MANAGER_ID desc ;
SELECT EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees where SALARY=‘17000’ ;#不好的示范,因为salary是数字型的
SELECT EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees where SALARY=17000 ;#用原有的类型
SELECT EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees where SALARY>17000;
SELECT EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees where SALARY>=5000 AND SALARY<=17000;
SELECT EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees where SALARY BETWEEN 5000 AND 17000
SELECT EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees where SALARY>5000 AND SALARY<17000;# 之间的,不包含
SELECT EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees where SALARY>17000 OR FIRST_NAME=‘Lex’
SELECT EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees where NOT SALARY=17000 ;
SELECT EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees where SALARY!=17000 ;
SELECT EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees where SALARY<>17000 ;
select FIRST_NAME,MANAGER_ID from t_employees where MANAGER_ID is null ;
select FIRST_NAME,MANAGER_ID from t_employees where MANAGER_ID is not null ; # ``有值
select EMPLOYEE_ID,FIRST_NAME,COMMISSION_PCT from t_employees where COMMISSION_PCT is not null;
select EMPLOYEE_ID,FIRST_NAME,DEPARTMENT_ID from t_employees WHERE
DEPARTMENT_ID =‘70’ or DEPARTMENT_ID =‘80’ or DEPARTMENT_ID =‘90’;
#枚举查询
select EMPLOYEE_ID,FIRST_NAME,DEPARTMENT_ID from t_employees WHERE
DEPARTMENT_ID in (‘70’,‘80’,‘90’);
SELECT * from t_employees where FIRST_NAME like ‘L%’;
SELECT * from t_employees where FIRST_NAME like ‘L___’; #三个_
SELECT EMPLOYEE_ID,FIRST_NAME from t_employees where FIRST_NAME like ‘%a’;
SELECT EMPLOYEE_ID,FIRST_NAME,SALARY,
case
when SALARY>10000 then ‘A’
when SALARY>8000 then ‘B’
when SALARY>6000 then ‘C’
when SALARY>4000 then ‘D’
else ‘E’
end as ‘薪资分类’
from t_employees
select sysdate();
select CURDATE();
select CURTIME();
select now();
select week(create_date) from t_date;
select year(now());
select minute(now());
select hour(now());
select DATEDIFF(create_date,now()) from t_date;
select ADDDATE(now(),10);
select * from t_date where create_date > ‘2019-01-01’
#员工的姓和名拼起来
select concat(FIRST_NAME,’_’,LAST_NAME) as totalName from t_employees;
select upper(FIRST_NAME) from t_employees;
select insert(‘这是一个数据库’,3,2,‘JAVA’);
select SUBSTRING(‘这是一个数据库’,1,2);
select sum(salary) from t_employees ;
select avg(salary) from t_employees;
#查询员工的最大薪资
select max(salary) from t_employees;
select min(salary) from t_employees;
select count(salary) from t_employees;
select count(*) from t_employees;#性能不好
select count(1) from t_employees;
select count(employee_id) from t_employees;
select count(MANAGER_ID) from t_employees;# 将null值行抹掉再计算
select * from t_employees
select count(employee_id),DEPARTMENT_ID,EMPLOYEE_ID as ‘没意义’ from t_employees GROUP BY DEPARTMENT_ID
select avg(salary) ,max(salary),min(salary),count(employee_id), DEPARTMENT_ID from t_employees group by DEPARTMENT_ID;
select avg(salary),DEPARTMENT_ID,JOB_ID from t_employees group by DEPARTMENT_ID,JOB_ID
select count(employee_id),DEPARTMENT_ID from t_employees GROUP BY DEPARTMENT_ID HAVING count(EMPLOYEE_ID)>1
select DEPARTMENT_ID , max(salary) from t_employees GROUP BY DEPARTMENT_ID having DEPARTMENT_ID in (‘60’,‘70’,‘80’);
select DEPARTMENT_ID , max(salary) from t_employees where DEPARTMENT_ID in (‘60’,‘70’,‘80’) GROUP BY DEPARTMENT_ID;
select employee_id,salary from t_employees order by salary desc limit 1;
select employee_id,salary from t_employees order by salary desc limit 0,5 ;# 第一个是下标,从0开始,代表取第几行数据,第二个是取几个值
select employee_id,salary from t_employees order by salary desc limit 5,5 ;
select employee_id,salary from t_employees order by salary desc limit 10,5 ;
select employee_id,salary from t_employees order by salary desc limit 2,5 ;
select EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees where
SALARY > (select salary from t_employees where FIRST_NAME =‘Bruce’);
select EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees where DEPARTMENT_ID in
(
select DEPARTMENT_ID from t_employees where LAST_NAME =‘King’);
select EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees where
SALARY > (select salary from t_employees where DEPARTMENT_ID =‘60’);
select EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees where
SALARY > all (select salary from t_employees where DEPARTMENT_ID =‘60’);
select EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees where
SALARY > (select max(salary) from t_employees where DEPARTMENT_ID =‘60’); #返回单行
select EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees where
SALARY > any (select salary from t_employees where DEPARTMENT_ID =‘60’);
#大于某一个写法2
select EMPLOYEE_ID,FIRST_NAME,SALARY from t_employees where
SALARY > (select min(salary) from t_employees where DEPARTMENT_ID =‘60’);
select t1.EMPLOYEE_ID,t1.FIRST_NAME,t1.YEARSALARY from (
select EMPLOYEE_ID,FIRST_NAME,SALARY*12 as YEARSALARY from t_employees
) as t1
order by t1.YEARSALARY desc limit 0,5
;
select * from t_employees
inner join t_departments
select t_employees.employee_id,t_employees.first_name ,t_departments.department_name from t_employees
inner join t_departments on t_employees.DEPARTMENT_ID = t_departments.DEPARTMENT_ID
select t_employees.employee_id,t_employees.first_name ,t_departments.department_name from t_employees
left join t_departments on t_employees.DEPARTMENT_ID = t_departments.DEPARTMENT_ID
select t_employees.employee_id,t_employees.first_name ,t_departments.department_name from t_employees
right join t_departments on t_employees.DEPARTMENT_ID = t_departments.DEPARTMENT_ID;
select t_employees.EMPLOYEE_ID,t_jobs.JOB_TITLE,t_departments.DEPARTMENT_NAME from t_employees
inner join t_jobs on t_jobs.JOB_ID = t_employees.JOB_ID
inner join t_departments on t_departments.DEPARTMENT_ID = t_employees.DEPARTMENT_ID