如:select distinct name from student;
如:select name,salary from employee where deptno = 3;
如:select * from employees where deptno = 3 and salary>-5000;(两个条件同时满足)
select * from employees where (deptno =3 or depton =1) and salary < 5000;
如:select * from dept where loc like '一楼%';(表示查找loc字段中以一楼开头的数据,显示满足条件的所有字段对应的信息)。类似的还有%一楼%(数据中包含一楼的),%一楼(数据以一楼结尾的)
如:select name from employees where empno not in (3,5);
select name from employees where deptno in (select depton from dept where loc like '一楼%');
如:select name from employees where hiredate between '2013-01-01' and '2013-12-31';
否定句:select name from employees where hiredate not between '2013-01-01' and '2013-12-31';
如:select name, hiredate from employees order by hiredate;(查询employees表中的name,hiredate字段的所有数据,按照hiredate字段进行默认的降序排序) 注意:order by 要写在条件或分组的后面,因为要先查询出需要的数据,才会对需要的数据进行排序。
如:update employees set deptno = 2,manager = 4 where empno =4;(更新employees表中empno=4的相应字段的数据)
如:delete from employees where empno = 9;(删除employees表中empno=9的那行数据)
如:create index in_name on employees(name);(在employees表中的name字段上创建一个名为in_name的索引)
如:创建一个名为employees_2015的视图,视图内容为hiredate<'2015-01-01'的name、salary字段的数据
create view employees_2015 as select name,salary from employees where hiredate < '2015-01-01';
注意:修改视图的数据,本质上是从底层进行修改,所以修改视图的数据,原始数据也被修改了。
如:select * from employees where manager is not null;
如:select name 姓名, salary 工资 from employees e;(表示设置字段name的别名为姓名,salary的别名为工资,表employees的别名为e)
注意:设置别名还可以用于函数中,如:
select count(*) 总人数 from employees;
内连接:两个表同时满足条件的数据,即交集。
左连接:左表中所有数据+右表中符合条件的数据。
右连接:右表中所有数据+左表中符合条件的数据。
如:
内连接:select name,title from employees inner join managers on employees.empno = managers.empno;
左连接:select name,title from employees left join managers on employees.empno = managers.empno;
内连接:select name,title from employees right join managers on employees.empno = managers.empno;
也就是嵌套查询
如:select name from employees where deptno in (select deptno from dept where loc like '二楼%');
select * from managers where empno not in (select empno from employees);
SQL 中五个常用函数:count()、max()、min()、avg()、sum();
1)案例实操: 文章来源:https://www.toymoban.com/news/detail-416767.html
hive (default)> select t.deptno, avg(t.sal) avg_sal from emp t group by
t.deptno;
hive (default)> select t.deptno, t.job, max(t.sal) max_sal from emp t
group by t.deptno, t.job;
注意:having和where的功能差不多,但处理的对象不同,having专门处理group by分组的数据,通常情况下,有group by的语句中才会有having。
hive (default)> select deptno, avg(sal) from emp group by deptno;
hive (default)> select deptno, avg(sal) avg_sal from emp group by deptno
having avg_sal > 2000;
参考b站视频:38分钟学会SQL语句sql_哔哩哔哩_bilibili文章来源地址https://www.toymoban.com/news/detail-416767.html
到了这里,关于基础SQL语法语句大全(一篇学会所有SQL语句)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!