ORACLE database summary 03

field alias

When a function or expression is used in the SELECT clause

, the name of the field in the result set is this function

Or the expression, which is poorly readable, can be used at this time

alias.

When spaces or case sensitivity are expected in aliases,

Can be enclosed in double quotes

SELECT ename,sal*12 "Annual Salary"

FROM emp

 

Query the information of employees whose salary is less than 2,000 yuan in the employee table

 

Query the information of employees who do not belong to department 10 in the employee table (<>)

 

Query the information of employees who joined after January 1, 1982 in the employee table

 

 

AND and OR join multiple conditions

View CLERK and SALESMAN with salary above 1000

SELECT ename,job,sal

FROM emp

WHERE sal>1000

AND (job='SALESMAN'

OR job='CLERK')

 

AND has higher priority than OR, so if you want to increase OR's

precedence, parentheses are required.

 

 

The LIKE keyword is used to add fuzzy matching strings

condition, supports two wildcards:

"_": means any character

"%": means any number of characters (0-more)

 

See if the second letter is A and the fourth letter is T?

SELECT ename,sal,job

FROM emp

WHERE ename LIKE '_A_T%'

 

 

IN(list),NOT IN(list)

Determine if it is in the list or not in the list

IN is often used in the judgment of subqueries

 

SELECT ename, job 

FROM emp  

WHERE 

 job NOT IN ('MANAGER', 'CLERK')

 

BETWEEN... AND ...

Judging between the two

View salary between 1500 to 3000 (inclusive)

SELECT ename, sal 

FROM emp  

WHERE 

 sal BETWEEN 1500 AND 3000

 

 

ANY(list),ALL(list)

When it is necessary to judge >, >=, <, <= in a separate table

When data, it should be used with ANY and ALL

Usually, the content of judgment is not a definite value, but is used in

Judge the result of the subquery.

>ANY: greater than the smallest

<ANY: less than the largest

>ALL: greater than the largest

<ALL: less than the smallest

SELECT empno, ename, job, sal, deptno

FROM emp

WHERE sal > ANY (3500,4000,4500);

 

DISTINCT can specify fields in the result set

To remove duplicate records of values, DISTINCT should

Immediately after SELECT.

 

See what positions are available at the company?

SELECT DISTINCT job

FROM emp

 

 

DISTINCT deduplicates multiple fields

The principle of deduplication: the combination of multi-field values ​​is not repeated

SELECT DISTINCT job,deptno

FROM emp

 

result set sorting

ORDER BY clause

ORDER BY can be based on the fields specified after it

Sort in ascending or descending order

The ORDER BY clause can only be written in the SELECT statement

on the last clause.

 

View company salary rankings

SELECT ename,sal,deptno

FROM emp

ORDER BY sal DESC

 

There is priority when sorting multiple fields, ORDER BY

will sort the result set by the first field, when the first

When the value of the field is repeated, the second field will be used

Sort these records, and so on.

SELECT ename,sal,deptno

FROM emp

ORDER BY deptno,sal DESC

 

If the sorted field contains NULL values, NULL

considered to be the maximum

SELECT ename,comm

FROM emp

ORDER BY comm DESC

 

 

 

 

Aggregate function

Used to count the value of the specified field in the result set

 

MAX, MIN: find the maximum and minimum values

 

Check the company's maximum and minimum wages?

SELECT MAX(sal),MIN(sal)

FROM emp

 

View the company's average salary and total salary?

SELECT AVG(sal),SUM(sal)

FROM emp

 

Aggregate functions ignore NULL values

SELECT NVL(comm,0)

FROM emp

 

SELECT AVG(NVL(comm,0)),SUM(comm)

FROM emp

 

COUNT function

COUNT counts records whose specified field is not NULL

total

 

How many people are there in the company?

SELECT COUNT(ename)

FROM emp

 

Usually, you can use COUNT(*) to count the number of records

 

See the number of people whose job title is "MANAGER" in the company?

SELECT COUNT(*)

FROM emp

WHERE job='MANAGER'

 

See what is the average salary for CLERK in a company?

SELECT AVG(sal)

FROM emp

WHERE job='CLERK'

 

 

grouping

GROUP BY clause

The GROUP BY clause can group the query result set according to the following

Records with the same field value specified are grouped together.

The purpose of grouping is to cooperate with aggregate functions for detailed statistical work

 

See the average salary for each position?

SELECT AVG(sal),job

FROM emp

GROUP BY job

 

When aggregate functions are used in the SELECT clause, then

Any single field that is not in an aggregate function must be present

In the GROUP BY clause, the reverse is not necessary

 

SELECT MAX(sal),MIN(sal),deptno

FROM emp

GROUP BY deptno

 

SELECT COUNT(*),job

FROM emp

GROUP BY job

 

View the average salary for a department, if the department's

Average salary above 2500

SELECT AVG(sal),deptno

FROM emp

WHERE AVG(sal)>2500

GROUP BY deptno

 

Aggregate functions cannot be used in the WHERE clause as

Filter conditions, because the filter timing is wrong.

WHERE is the process of getting the result set in the query table

Filter the data in the table one by one to form a result set.

Aggregate functions are based on the result set,

So filtering using aggregate function results is done in WHERE

Afterwards.

 

HAVING clause

The HAVING clause must follow the GROUP BY clause

HAVING can use aggregate functions as filter conditions

HAVING determines the choice of GROUP BY grouping

 

SELECT MAX(sal),MIN(sal),deptno

FROM emp

WHERE job='MANAGER'

GROUP BY deptno

HAVING AVG(sal)>2500

ORDER BY deptno

 

SELECT sal,deptno

FROM emp

ORDER BY deptno

 

Related query

The fields in the result set come from multiple table association queries.

 

When the fields of the query are in multiple tables, it needs to be clear

The specified field is from which table, and the table can define an alias, to

Simplify the complexity of SQL statements.

 

View information about each employee and their department

SELECT e.ename,e.deptno,d.dname

FROM emp e,dept d

WHERE e.deptno=d.deptno

 

Multi-table association queries usually need to add join conditions, otherwise

produces a Cartesian product, which is usually a meaningless result

set, and the overhead is huge.

SELECT e.ename,d.dname

FROM emp e,dept d

 

The filter condition must be established at the same time as the join condition

View employees in the SALES department?

SELECT e.ename,e.sal,e.job,d.dname

FROM emp e,dept d

WHERE e.deptno=d.deptno

AND d.dname = 'SALES'

 

Joint query of N tables, at least N-1 join conditions

 

 

Internal link

SELECT e.ename,d.dname

FROM emp e JOIN dept d

ON e.deptno=d.deptno

WHERE d.dname='SALES'

 

A relational query ignores records that do not meet the join condition

 

 

outer join

In addition to the connection query, outer join can be

In addition to querying records that meet the connection conditions, also

You can query records that do not meet the connection conditions

Outer joins are divided into:

Left outer join: use the table on the left side of the JOIN as the driving table

        The data in the drive table must be displayed, then

        When a record does not meet the connection conditions, then

        All fields from the table on the right side of the JOIN are filled with NULL values

Right outer join:

Full outer join:

 

SELECT e.ename,e.job,d.dname,d.loc

FROM emp e 

  LEFT|RIGHT|FULL OUTER JOIN 

     dept d

ON e.deptno=d.deptno

 

 

SELECT e.ename,e.job,d.dname,d.loc

FROM emp e,dept d

WHERE e.deptno(+)=d.deptno

 

self-connection

Self-connection is designed to solve the problem of saving data of the same nature

However, it is used when there is tree-structured data with a superior-subordinate relationship

 

SELECT empno,ename,mgr

FROM emp

 

See the name of each employee and their boss?

SELECT e.ename,m.ename

FROM emp e JOIN emp m

ON e.mgr=m.empno

 

Who is the boss of FORD?

SELECT m.ename

FROM emp e JOIN emp m

ON e.mgr = m.empno

WHERE e.ename='FORD'

 

Find out which city does your boss at FORD work in?

SELECT e.ename,m.ename,d.loc

FROM emp e,emp m,dept d

WHERE e.mgr=m.empno

AND m.deptno=d.deptno

AND e.ename='FORD'

 

inner join

SELECT e.ename,m.ename,d.loc

FROM emp e JOIN emp m

ON e.mgr=m.empno

JOIN dept d

ON m.deptno=d.deptno

WHERE e.ename='FORD'

 

See the name of each employee and their boss?

SELECT e.ename,m.ename

FROM emp e JOIN emp m

ON e.mgr=m.empno

 

View KING's subordinates?

SELECT e.ename

FROM emp e,emp m

WHERE e.mgr=m.empno

AND m.ename='KING'

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326669819&siteId=291194637
Recommended