1、顯示當前所在用戶;
show user;
2、查詢用戶語言環境,借助dual偽表;
select userenv('language') from dual;
3、查看emp表結構;
desc emp;
4、查詢emp表所有資訊;
select * from emp;
5、使用全列名查詢emp表所有資訊;
select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp;
6、使用列別名查詢emp表所有資訊;
select empno 員工編號,ename 員工姓名,job 職位,mgr 上級,hiredate 雇傭日期,sal 工資,comm 獎金,deptno 部門編號 from emp;
7、查詢emp表員工編號為7876的所有資訊;
select * from emp where empno=7876;
8、查詢emp表消除重復職位資訊;
select distinct job from emp;
9、查詢emp表每個雇員的工資;
select ename 員工姓名,sal 工資 from emp;
10、查詢emp表每個雇員的年薪;
select ename 員工姓名,sal * 12 年薪 from emp;
11、查詢emp表員工編號、員工姓名、職位資訊,使用字串連接;
select '員工編號:' || empno || ',員工姓名:' || ename || ',職位:' || job || ' ' from emp;
12、查詢emp表每月能得到獎金的雇員;
select * from emp where comm is not null;
13、查詢emp表每月得不到獎金的雇員;
select * from emp where comm is null;
14、查詢emp表工資大于1500并且有獎金領取的雇員;
select * from emp where comm is not null and sal > 1500;
15、查詢emp表工資大于1500或者有獎金領取的雇員;
select * from emp where comm is not null or sal > 1500;
16、查詢emp表工資不大于1500和沒有獎金領取的雇員;
– 使用邏輯運算子 not
select * from emp where comm is null and not(sal > 1500);
– 不使用邏輯運算子 not
select * from emp where comm is null and sal <= 1500;
17、查詢emp表工資大于1500但小于3000的全部雇員;
– 方式一
select * from emp where sal > 1500 and sal < 3000;
– 方式二 使用 between and 閉區間
select * from emp where sal between 1500 and 3000 and (sal <> 1500 and sal != 3000);
18、查詢emp表1981-1-1到1981-12-31號入職的雇員;
select * from emp where hiredate between '1-1月-1981' and '31-12月-1981';
19、查看系統日期,借助dual偽表;
select sysdate from dual;
20、日期型別轉換成字串函式TO_CHAR()
select to_char(sysdate) from dual;
select to_char(sysdate,'yy-mm-dd') from dual;
select to_char(sysdate,'dd-mm-yyyy') from dual;
select to_char(sysdate,'yyyy-mm-dd') from dual;
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') 日期 from dual;
21、字串型別轉換為日期函式TO_DATE()
select to_date('2020-10-05 23:59:56','yyyy-mm-dd hh24:mi:ss') from dual;
select to_char(to_date('2020-10-06 23:34:45','yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24:mi:ss')日期 from dual;
22、查詢雇員名字叫 smith 的雇員,注意查詢條件的值是區分大小寫的
select * from emp where ename = 'smith';
23、查詢雇員名字叫 SMITH 的雇員,注意查詢條件的值是區分大小寫的
select * from emp where ename = 'SMITH';
24、查詢雇員編號是7369,7499,7521的雇員編號的具體資訊
(1)使用邏輯運算子 and 無效 無效 無效
select * from emp where (empno = 7369) and (empno = 7499) and (empno = 7521);
(2)使用邏輯運算子 or 可以,加上括號更好一些
select * from emp where (empno = 7369) or (empno = 7499) or (empno = 7521);
select * from emp where empno = 7369 or empno = 7499 or empno = 7521;
(3)使用 IN 關鍵字
select * from emp where empno in (7369,7499,7521);
25、查詢雇員姓名是 ‘SMITH’,‘ALLEN’,'WARD’的雇員具體資訊
select * from emp where ename in ('SMITH','ALLEN','WARD');
select * from emp where (ename = 'SMITH') or (ename = 'ALLEN') or (ename = 'WARD');
–模糊查詢 LIKE ‘%’:任意長度, _’:一個長度
26、查詢所有雇員姓名中第二個字符包含“ M ”的雇員
select * from emp where ename like '_M%';
在LIKE中如果沒有關鍵字表示查詢全部
select * from emp where ename like '%';
27、查詢所有雇員姓名中包含“ M ”的雇員
select * from emp where ename like '%M%';
28、查詢雇員編號不是7369的雇員資訊
select * from emp where empno <> 7369;
select * from emp where empno != 7369;
–使用 order by 對結果排序,升序ASC 默認,降序DESC
29、查詢emp表雇員的工資,從低到高
select * from emp order by sal;
30、查詢雇員的工資,從高到低
select * from emp order by sal desc;
31、如果存在多個排序欄位可以用逗號分隔
select * from emp order by sal asc, hiredate desc;
–排序中的空值問題,可以使用nulls first, nulls last來指定null值顯示的位置
32、查詢雇員的工資從低到高
select * from emp order by sal nulls first;
33、查詢雇員的工資從高到低(使用nulls last)
select * from emp order by sal desc nulls last;
都看到最后了啦,如果覺得寫得好的話吶
記得 一鍵三連 哦!點贊 也行吶!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/172915.html
標籤:其他
下一篇:MySQL初學筆記(第一次作業)
