在这里插入代码片
@[TOC]
预备知识
SQL(Structured Query Language)是一种用于管理关系型数据库的标准化语言。它允许用户通过简单的命令来查询、插入、更新和删除数据库中的数据。
SQL查询是使用SELECT语句从数据库中检索数据的过程。它可以根据特定的条件过滤数据,排序结果,并使用聚合函数计算汇总数据。以下是SQL查询的一些常见用法:
SELECT语句:SELECT语句用于从数据库中选择要检索的列。例如,SELECT column1, column2 FROM table; 将返回表中指定列的数据。
WHERE子句:WHERE子句用于过滤数据,只返回满足特定条件的行。例如,SELECT * FROM table WHERE column = value; 将返回满足指定条件的行。
ORDER BY子句:ORDER BY子句用于对结果进行排序。例如,SELECT * FROM table ORDER BY column ASC; 将按升序对结果进行排序。
GROUP BY子句:GROUP BY子句用于将结果按照指定的列进行分组。例如,SELECT column, COUNT(*) FROM table GROUP BY column; 将按照指定列的值分组,并计算每组的行数。
HAVING子句:HAVING子句用于对分组后的结果进行过滤。例如,SELECT column, COUNT() FROM table GROUP BY column HAVING COUNT() > 10; 将返回满足指定条件的分组结果。
JOIN操作:JOIN操作用于将两个或多个表中的数据进行关联。例如,SELECT * FROM table1 JOIN table2 ON table1.column = table2.column; 将返回两个表中列值相等的行。
子查询:子查询是在主查询中嵌套的查询语句。它可以作为主查询的条件、过滤或计算结果的一部分。例如,SELECT * FROM table WHERE column IN (SELECT column FROM table2); 将返回满足子查询条件的行。
SQL查询是数据库管理的核心部分,它可以帮助用户从数据库中检索需要的数据,并根据特定的需求进行处理和分析。
select upper(substring(last_name,1,1)) + lower(substring(last_name,2)) as 'Last Name',
length(last_name) as 'Name Length'
from employees
where last_name like 'J%' or last_name like 'A%' or last_name like 'M%'
order by last_name;
select last_name, round(datediff(current_date,hire_date)/30) as 'MONTHS_WORKED'
from employees
order by MONTHS_WORKED;
select last_name, LPAD(concat('$',salary),15,'$') as 'SALARY'
from employees;
select last_name,hire_date,(DAYOFWEEK(hire_date)+5) %7 + 1 as 'DAY'
from employees;
select last_name,
case
when commission_pct is null then 'No Commission'
else concat(commission_pct*100,'%')
end as 'COMM'
from employees;
select MAX(salary) as 'maximum',
MIN(salary) as 'minimu',
SUM(salary) as 'sum',
round(avg(salary)) as 'average'
from employees;
select MAX(salary) as 'maximum',
MIN(salary) as 'minimu',
SUM(salary) as 'sum',
round(avg(salary)) as 'average'
from employees
group by job_id;
select job_id,count(*) as 'Number of Employees'
from employees
group by job_id;
select job_id,count(*) as 'Number of Managers'
from employees
where job_id='IT_PROG' or job_id='ST_MAN'
select max(salary)-min(salary) as 'Salary Difference'
from employees;
select d.department_name,d.location_id,count(e.employee_id) as 'Number of Employees',
round(avg(e.salary0,2)as 'Average Salary'
from departments d
join employees e on d.department_id=e.department_id
group by d.department_name,d.location_id;
select e.last_name,e.department_id,d.department_name
from employees e
join departments d on e.department_id=d.department_id;
select distinct job_id,location_id
from employees
where department_id=80;
select last_name,department_name
from employees
join departments on employees.department_id = departments.department_id
where last_name like '%a%';
select e.last_name,e.job_id,e.department_id,d.department_name
from employees e
join departments d on e.department_id = d.department_id
where d.location_id = 'Toronto';
select e.last_name aas 'employee',e.employee_id as 'emp_id',m.last_name as 'manager',e.manager_id as 'mgr_id'
from employees e
join employees m on e.manager_id = m.employee_id;
select e.last_name as 'last_names',e.department_id
from employees e
join employees m on e.department_id =m.department_id
where m.last_name = '指定雇员的last_name';
select last_name,hire_date
from employees
where department_id = (select department_id from employees where last_name ='zlotkey')
and last_name !='zlotkey';
select employee_id,last_name
from employees
where salary>(select avg(salary) from employees)
order by salary asc;
select last_name,department_id,job_id
from employees
where department_id in (select department_id from departments where location_id = 1700);
select department_id,last_name,job_id
from employees
where department_id=(select department_id from departments where department department_name='Executive');
以上是今天要讲的内容,学到了SQL的基础语句以及相关练习。