预备知识
SQL(Structured Query Language)是一种用于管理关系数据库的编程语言。它允许用户在数据库中存储、修改和检索数据,并提供了一些强大的功能,如数据过滤、排序、分组和聚合。SQL是一种标准化的语言,几乎所有的关系数据库管理系统都支持它。
SQL的用法包括以下几个方面:
使用SQL可以创建和删除数据库。创建数据库的语法如下:
CREATE DATABASE database_name;
删除数据库的语法如下:
DROP DATABASE database_name;
使用SQL可以创建和删除表。创建表的语法如下:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
.....
);
删除表的语法如下:
DROP TABLE table_name;
使用SQL可以插入、更新和删除数据。插入数据的语法如下:
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
更新数据的语法如下:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
删除数据的语法如下:
DELETE FROM table_name WHERE condition;
使用SQL可以查询数据。查询数据的语法如下:
SELECT column1, column2, ... FROM table_name WHERE condition;
使用SQL可以对数据进行过滤、排序、分组和聚合。过滤数据的语法如下:
SELECT column1, column2, ... FROM table_name WHERE condition;
排序数据的语法如下:
SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC|DESC;
分组数据的语法如下:
SELECT column1, column2, ... FROM table_name GROUP BY column1, column2, ...;
聚合数据的语法如下:
SELECT COUNT(column_name) FROM table_name WHERE condition;
SELECT SUM(column_name) FROM table_name WHERE condition;
SELECT AVG(column_name) FROM table_name WHERE condition;
SELECT MAX(column_name) FROM table_name WHERE condition;
SELECT MIN(column_name) FROM table_name WHERE condition;
总之,SQL是一种强大的数据库管理语言,它可以帮助用户管理和处理大量的数据。熟练掌握SQL的用法,可以提高数据管理的效率和准确性。
SELECT last_name, job_id, salary AS Sal FROM employees;
select语句执行成功了
它选择了employees表中last_name,job_id,salary列,并将salary列的别名设置为Sal.
SELECT employee_id, last_name salx12 ANNUAL SALARY FROM employees;
修正后的语句:
select employee_id,last_name,'ANNUAL SALARY' as salx12 from employees;
describe DEPARTMENTS;
describe employees;
select employee_id,last_name,job_id,hire as startdate
from employees;
select distinct job_id from employees;
select last_name,salary
from employees
where salary>12000;
select e.last_name,d.department_id
from employees e
join departments d on e.department_id = d.department_id
where e.employee_id = 176;
select last_name,salary
from employees
where salary not between 5000 and 12000;
select last_name,job_id,hire_date
from employees
where hire_date between '1998-02-20' and '1998-05-01'
order by hire_date desc;
select last_name,department_id
from employees
where department_id in (20,50)
order by last_name asc;
select last_name as employee,salary as 'Monthly Salary'
from employees
where salary between 5000 and 12000
and department_id in (20,50);
select last_name,hire_date
from employees
where hire_date between '1994-01-01' and '1994-12-31';
select last_name,job_id
from employees
where manager_id is null;
select last_name,salary,commission_pct
from employees
where commission_pct is not null
order by salary desc,commission_pct desc;
select last_name
from employees
where last_name like '__a%';
select last_name
from employees
where last_name like '%a%e%';
select last_name,job_id,salary
from employees
where (job_id = 'SA_REP' or job_id ='ST_CLERK')
and salary not in (2500,3500,7000);
select current_date as 'DATE';
select employee_id, last_name, salary, round(salary*1.15) as 'New Salary'
from employees;
select employee_id,last_name,salary,
round(salary*1.15) as 'New Salary',
round((salary*1.15)-salary) as increase
from employees;
以上是今天要讲的内容,学到了SQL的基础语句练习。