Simple query statement of Oracle


Instructions that display the time required for
each command sql>desc dept;






2) Query all columns (so the query efficiency is low)
sql>select * from dept;


3) Query the specified column (so the query is more efficient than select *)
sql>select ename,sal,job,deptno from emp;


4) How to cancel duplicate rows
sql>select distinct deptno,job from emp;


5) Query the amount of data in the database
sql>select count(*) from table;

6) How to use arithmetic expressions

? Display the annual salary of each employee

SQL> select ename,sal*12 from emp;


7) Use the alias of the column (add "alias" directly after the attribute)
sql>select ename "name",sal*12 as "year Income" from emp;

8) How to deal with null values ​​(as long as there is a null component in the arithmetic expression, the entire arithmetic expression is a null value)

For example:
SQL> select ename "name",sal*12+comm*12 "Annual salary" from emp;
In the arithmetic expression sal*12+comm*12, the bonus represented by comm, some people have no bonus, that is, the value of comm of these people is null, then the sal*12+comm* of these people The value of 12 is null

. Use the nvl function to process
SQL> select ename "name", sal*12+nvl(comm,0)*12 "annual salary" from emp;
where nvl(comm,0) means: if If comm finds a null value, then replace it with 0.


9) How to connect the string (||)
sql>select ename || 'is a' || job from emp;



How to quickly increase the data in the data and use auto-increment !
For example:
SQL>
//In this way, the data in the database will increase exponentially

 

1) Use where clause
? How to display employees with salary higher than 3000
SQL> select * from emp where sal>3000;

? How to find employees who joined after 1982.1.1
SQL> select * from emp where hiredate>'January-January-1982';
or change the date format first and then search
for SQL> alter session set nls_date_format='yyyy-mm-dd';
Session altered

SQL> select * from emp where hiredate>'1982-1-1';

? How to display the situation of employees whose salary is between 2000 and 2500
Method 1:
SQL> select * from emp where sal between 2000 and 2500;
this is including 2000 and 2500.
If 2000 is not required, add and sal!=2000
if 2000 is not required And 2500 is on the back home and sal!=2000 and sal!=2500;


Method 2:
SQL> select * from emp where sal>=2000 and sal<=2500;

2) How to use the like operator
%: indicates 0 to multiple characters
_: indicates a single character


? How to display the name and salary of the employee whose first character is S
SQL> select ename,sal from emp where ename like 'S%';

? How to display the name and salary of the employee whose third character is N
SQL> select ename,sal from emp where ename like '__N%';


3) Use in in where condition
? How to use the employee situation where empno is 123 45 800...
SQL> select * from emp where empno in(123,45,800);

4) Use the operator of is null
? How to display employees who have no superiors?
In particular, there is a column in the data table that is used to put mgr (superior)
SQL> select * from emp where mgr is null;

 

1) Using logical operators
? Query employees whose salary is higher than 500 or whose position is MANAGER, and also satisfy J
SQL> select * from emp where (sal>500 or job='MANAGER') and ename like 'J% ';
To add a parenthesis, otherwise the latter one is not a constraint


2) Use the order by clause (the default is ascending order, if followed by desc, it becomes descending order)
The default sorting order is ascending ASC. If you want to sort in descending order, you must write the DESC keyword.
Note : If you omit ASC and DESC, the default is ASC, that is, ascending order.


? How to display employee information in order of salary from low to high
SQL> select * from emp order by sal;


? How to display employee information in order of salary from high to low
SQL> select * from emp order by sal desc;


? Sort by department number in ascending order and employee salary in descending order
SQL> select * from emp order by deptno,sal desc;


? Sort by department number in ascending order and employee salary in ascending order
SQL> select * from emp order by deptno,sal;


? Sort by department number in ascending order and employees' entry time in descending order
SQL> select * from emp order by deptno,hiredate desc;



3) Sort by column alias
SQL>select ename,sal*12 "annual salary" from emp order by "annual salary" asc ;

Aliases need to be circled with "", if it is a Chinese alias, it can be circled with ''.

Guess you like

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