【sql】訓練二

不一樣的天空w發表於2016-10-25

0、顯示正好為5個字元的員工的姓名

   SQL> select concat(first_name,last_name) name from employees where length(concat(first_name,last_name))=5;

 

NAME

---------------------------------------------

KiGee

或者

SQL> select concat(first_name,last_name) name from employees where concat(first_name,last_name) like '_____';

 

NAME

---------------------------------------------

KiGee


1、  顯示不帶有"R"的員工的姓名.

 

SQL> select first_name from employees where first_name not like '%R%';

 

2、  顯示所有員工姓名的前三個字元.

SQL> select substr(first_name,1,3) name from employees;

 

3、  顯示所有員工的姓名,a替換所有"A"

SQL> select replace(first_name,'A','a') name from employees;

 

4、  顯示員工的詳細資料,按姓名排序

Desc employees;

Select * from employees order by 2;

或者

Select * from employees order by first_name;

5、  顯示員工的姓名和受僱日期,根據其服務年限,將最老的員工排在最前面.

SQL> select first_name,last_name,hire_date from employees

  2  order by 3 ;

6、  顯示所有員工的姓名、工作和薪金,按工作的降序排序,若工作相同則按薪金排序

SQL> select first_name,job_id,salary from employees order

    by salary desc,salary;

 

7、  顯示在一個月為30天的情況所有員工的日薪金,忽略餘數.

select first_name,trunc(salary/30,0) rixin from employees;

 

8、  顯示姓名欄位的任何位置包含"A"的所有員工的姓名.

SQL> select first_name from employees where first_name

    like ’%A%’;

 

9、  找出員工名字中含有ae

SQL> select first_name name from employees where

  2      first_name like '%a%e%';

1050部門有哪些職位

SQL> select job_id,first_name,department_id from employees

  2  where department_id =50;

 

11、除了Sales部門,還有哪些部門,列出部門號、部門名稱。

SQL> select department_id,department_name from departments

  2  where department_name !='Sales';

 

12select語句的輸出結果格式如下:

select * from hr_departments;

select * from hr_emp;

select * from hr_region;

…….

Departments是表名,可以查詢tab

SQL> desc tab;

 Name                                      Null?    Type

 ----------------------------------------- -------- ----------------------------

 TNAME                                     NOT NULL VARCHAR2(30)

 TABTYPE                                            VARCHAR2(7)

 CLUSTERID                                          NUMBER

SQL> select 'select * '|| 'from' || tname || ';' as "select" from tab;

 

13. 檢視每月可以得到獎金的僱員資訊

SQL> select first_name,salary*commission_pct jiangjin from

  2  employees where salary*commission_pct is not null;

 

14. 要求基本工資大於1500,同時可以領取獎金的僱員資訊

SQL> select first_name,salary,commission_pct from employees

  2  where salary >= 1500 and commission_pct is not null;

 

15. 查詢基本工資大於1500,同時不可以領取獎金的僱員資訊

SQL> select first_name,salary,commission_pct from employees

  2  where salary >= 1500 and commission_pct is null;

 

16.查詢在1981年僱傭的全部僱員資訊

SQL> select first_name,to_char(hire_date,'yyyy') hire from

  2  employees where to_char(hire_date,'yyyy') = 1981;

或者

SQL> select first_name,salary,hire_date from employees where

  2  hire_date between '01-JAN-81' and '31-DEC-81';

 

17.要求查詢出僱員編號不是 73697499的僱員資訊

SQL> select first_name,employee_id from employees where

  2  employee_id not in (7369,7499);

 

18. 檢視僱員編號不是7369的僱員資訊

SQL> select first_name,salary,employee_id from employees

  2  where employee_id != 7369;

 

19. 執行以下這個語句並解釋

SELECT substr('hello',1,3),length('hello'),replace('hello','l','x') FROM dual;

SQL> select substr('hello',1,3),length('hello'),replace('hello','l','x') FROM dual;

 

SUB LENGTH('HELLO') REPLA

--- --------------- -----

hel               5 hexxo

SUB欄位:將hello從第1位開始數3位輸出,其餘都截斷;

LENGTH('HELLO')欄位:數HELLO的字元長度輸出;

REPLA欄位:將hello字元中的l全部替換為x

 

20.說明substr從第0個字元和從第1個字元開始擷取欄位,有什麼區別

從第0個字元和從第1個字元開始擷取只要要擷取的位數是一樣的,則輸出結果就一樣:

示例如下:

SQL> select substr('hello',0,1) FROM dual;

 

S

-

h

 

SQL> select substr('hello',0,2) FROM dual;

 

SU

--

he

 

SQL> select substr('hello',0,0) FROM dual;

 

S

-

 

 

SQL> select substr('hello',1,0) FROM dual;

 

S

-

 

 

SQL> select substr('hello',1,1) FROM dual;

 

S

-

h

 

SQL>  select substr('hello',1,2) FROM dual;

 

SU

--

he

21. 要求顯示所有僱員的姓名及姓名的後3個字元

SQL> select last_name,substr(last_name,-3,3) name from employees;

 

22. 求出從僱用日期到今天所有僱員的僱員編號、姓名和月數

SQL> select first_name,employee_id,(sysdate-hire_date)/30 ys

  2  from employees order by 3;

 

23.說明以下語句最後輸出的結果:

SQL> SELECT ADD_MONTHS(SYSDATE,4) FROM DUAL;

SQL> SELECT NEXT_DAY(SYSDATE,'MON') FROM DUAL;

SQL> SELECT LAST_DAY(SYSDATE) FROM DUAL;

 

SQL>  SELECT ADD_MONTHS(SYSDATE,4) FROM DUAL;

 

ADD_MONTH

---------

03-JAN-17 2016-9-3,月數加4變成03-JAN-17

 

SQL>  SELECT NEXT_DAY(SYSDATE,'MON') FROM DUAL;

 

NEXT_DAY(

---------

05-SEP-16(意2016-9-3是星期六,求下一個星期一是幾號,則輸出05-SEP-16

 

SQL> SELECT LAST_DAY(SYSDATE) FROM DUAL;

 

LAST_DAY(

---------

30-SEP-16(意2016-9-3的這個月的最後一天是幾號,則輸出30-SEP-16

 

24. 求出每個僱員的年薪(應算上獎金)注意處理Null

SQL>  select first_name,12*(salary+salary*nvl(commission_pct,0)) as nianxin from employees order by 2;

或者

select first_name,salary*12+(salary*nvl(commission_pct,0))*12 nianxin from employees order by 2

 

 

25. 找出有獎金的員工的不同工作

SQL> select distinct job_id,commission_pct from

  2  employees where commission_pct is not null;

 

26. 找出在2月受聘的員工

SQL> select first_name,to_char(hire_date,'MM') hire from

  2  employees where to_char(hire_date,'MM') =2;

 

27.以年月日方式顯示所有員工的受聘日期。

SQL> select first_name,to_char(hire_date,'yyyy-mm-dd') hire

  2  from employees;

 

28.用concat顯示所有員工的姓名全稱

SQL> col FIRST_NAME for a10

SQL> col LAST_NAME for a10

SQL>  select first_name,last_name,concat(first_name,last_name) name from employees;

 

29.顯示所有員工姓名(last_name)倒數第三個字元。

SQL> select last_name,substr(last_name,-3,1) name from employees;

 或

select last_name,substr(last_name,length(last_name) -2,1) from employees;


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31397003/viewspace-2127033/,如需轉載,請註明出處,否則將追究法律責任。

相關文章