SELECT * FROM employees ORDER BY salary DESC;
SELECT * FROM employees ORDER BY salary;
SELECT *
FROM employees
WHERE department_id >= 90
ORDER BY hiredate ASC;
SELECT *,salary*12*(1+IFNULL(commission_pct,0)) 年薪
FROM employees
ORDER BY salary*12*(1+IFNULL(commission_pct,0)) DESC;
SELECT *,salary*12*(1+IFNULL(commission_pct,0)) 年薪
FROM employees
ORDER BY 年薪 DESC;
SELECT LENGTH(last_name) 字节长度,last_name,salary
FROM employees
ORDER BY LENGTH(last_name) DESC;
SELECT *
FROM employees
ORDER BY salary ASC,employee_id DESC;
SELECT last_name,department_id,salary*12*(1+IFNULL(commission_pct,0)) 年薪
FROM employees
ORDER BY 年薪 DESC,last_name ASC;
SELECT salary,last_name
FROM employees
WHERE salary NOT BETWEEN 8000 AND 17000
ORDER BY salary DESC;
SELECT *,LENGTH(email)
FROM employees
WHERE email LIKE '%e%'
ORDER BY LENGTH(email) DESC,department_id ASC;
SELECT LENGTH('john');
SELECT LENGTH('张三丰hehe');
SHOW VARIABLES LIKE '%char%'
SELECT CONCAT(last_name,'_',first_name) 姓名 FROM employees;
SELECT UPPER('john');
SELECT LOWER('JOHN');
SELECT CONCAT(UPPER(last_name),'_',LOWER(first_name)) 姓名 FROM employees;
SELECT SUBSTR('李莫愁是个大魔头',6) out_put;
SELECT SUBSTR('李莫愁是个大魔头',1,3) out_put;
SELECT
CONCAT(UPPER(SUBSTR(last_name,1,1)),'_',LOWER(first_name)) 姓名
FROM employees;
SELECT INSTR('abcdefghijk','hijk')AS out_put;
SELECT LENGTH(TRIM(' AAAA ')) AS out_put;
SELECT LPAD('aaaa',10,'*') AS out_put;
SELECT RPAD('aaaa',10,'*') AS out_put;
SELECT REPLACE('aaaaaaaaaaaa','a','b') AS out_put;
SELECT ROUND(-1.1);
SELECT ROUND(1.567,2);
SELECT CEIL(1.01);
SELECT FLOOR(9.99);
SELECT TRUNCATE(1.69,1);
SELECT MOD(-10,-3);
SELECT 10%3;
SELECT NOW();
SELECT CURDATE();
SELECT CURTIME();
SELECT YEAR(NOW()) 年;
SELECT YEAR('1991-1-1') 年;
SELECT YEAR(hiredate) 年 FROM employees;
SELECT MONTH(NOW()) 月;
SELECT MONTHNAME(NOW()) 月;
SELECT STR_TO_DATE('1991-8-21','%Y-%c-%d') out_put;
SELECT * FROM employees WHERE hiredate = '1992-4-3';
SELECT *
FROM employees
WHERE hiredate = STR_TO_DATE('4-3 1992','%c-%d %Y');
SELECT DATE_FORMAT(NOW(),'%Y年%m月%d日')AS out_put;
SELECT
last_name,DATE_FORMAT(hiredate,'%m月/%d日 %Y年') 入职日期
FROM employees
WHERE commission_pct IS NOT NULL;
SELECT IF(10<5,'大','小');
SELECT
last_name,commission_pct,
IF(commission_pct IS NULL,
'没奖金','有奖金') 备注
FROM employees;
SELECT salary 原始工资,department_id,
CASE department_id
WHEN 30 THEN salary*1.1
WHEN 40 THEN salary*1.2
WHEN 50 THEN salary*1.3
ELSE salary
END AS 新工资
FROM employees;
SELECT salary,
CASE
WHEN salary>20000 THEN 'A'
WHEN salary>15000 THEN 'B'
WHEN salary>10000 THEN 'C'
ELSE 'D'
END AS 工资级别
FROM employees;
SELECT SUM(salary) FROM employees;
SELECT AVG(salary) FROM employees;
SELECT MAX(salary) FROM employees;
SELECT MIN(salary) FROM employees;
SELECT COUNT(salary) FROM employees;
SELECT MAX(salary) mx_sal,MIN(salary) mi_sal,
ROUND(AVG(salary),2) ag_sal,SUM(salary) sm_sal
FROM employees;
SELECT DATEDIFF(MAX(hiredate),MIN(hiredate)) diffrence
FROM employees;
SELECT COUNT(*)
FROM employees
WHERE department_id=90;
SELECT AVG(salary) FROM employees;
SELECT MAX(salary),job_id
FROM employees
GROUP BY job_id;
SELECT COUNT(*),location_id
FROM departments
GROUP BY location_id;
SELECT AVG(salary),department_id
FROM employees
WHERE email LIKE '%a%'
GROUP BY department_id
SELECT MAX(salary),manager_id
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY manager_id;
SELECT COUNT(*),department_id
FROM employees
GROUP BY department_id;
SELECT COUNT(*),department_id
FROM employees
GROUP BY department_id
HAVING COUNT(*)>2;
SELECT MAX(salary),job_id
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY job_id;
SELECT MAX(salary),job_id
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY job_id
HAVING MAX(salary)>12000;
SELECT MIN(salary),manager_id
FROM employees
GROUP BY manager_id
SELECT MIN(salary),manager_id
FROM employees
WHERE manager_id>102
GROUP BY manager_id;
SELECT MIN(salary),manager_id
FROM employees
WHERE manager_id>102
GROUP BY manager_id
HAVING MIN(salary)>5000;
SELECT
COUNT(*),
LENGTH(last_name) len_name
FROM employees
GROUP BY LENGTH(last_name);
SELECT COUNT(*),LENGTH(last_name) len_name
FROM employees
GROUP BY len_name
HAVING COUNT(*)>5;
SELECT AVG(salary),department_id,job_id
FROM employees
GROUP BY department_id,job_id;
SELECT AVG(salary),department_id,job_id
FROM employees
WHERE department_id IS NOT NULL
GROUP BY department_id,job_id
HAVING AVG(salary) > 10000
ORDER BY AVG(salary) DESC;