SQL query operation two

1. Pseudo column: A virtual column, a column that does not exist in the original table, can be queried through the select statement.

I. Syntax: select pseudo column  from table name

II. Detailed explanation:

rowid [understand]: uniquely identifies the physical location of a row of data. //Oracle query optimizer, used more in internal query, rarely used in application development

rownum [Key points]: Assign a logical number to each row of the queried data that meets the requirements. //Start from 1 and increment by 1.

III. Example:

//Query a row of data based on rowid (fast)

select * from employees where rowid = 'AAAR5kAAFAAAADNABD'  file# block# row#

 

//Query all columns and rowids (table alias e, as is not allowed)

select e.*, rowid from employees e

 

//Query the top 5 employee information (number, name, salary) in the employee table

select employee_id , first_name , salary

from employees

where rownum <= 5;

 

//Query employee information after the 6th in the employee table (number, name, salary)

select employee_id , first_name , salary , rownum

from employees

where rownum > 5; //error has no matching results

Note: rownum is always generated from 1, it is only used in the environment of <=, and it is invalid in the environment of >=


//Query the information of the top 5 employees in the employee table by salary (number, name, salary)

select employee_id , first_name , salary

from employees

where rownum <= 5

order by salary desc //error

Reason: order by takes effect after where

 

Unfinished to be continued subquery

 

2. Subqueries: Nested Queries

I. Syntax: Same as basic query.

II. Detailed explanation:

Use the result of the subquery "one row, one column" as a conditional judgment to do the second query.

Use the result of the subquery "multiple rows and one column" as the judgment condition of the enumeration query to do the second query.

The result of the subquery "multiple rows and multiple columns" is used as a temporary table for the second query.

III. Example:

//Query the information of employees whose salary is greater than the average salary (job number, name, salary)

Ideas:

1). First query the average salary of employees

select avg(salary) from employees; //average salary: 6461.83177570093

 

2). Query the information of employees whose salary is greater than the average salary

select employee_id , first_name , salary

from employees

where salary > 6461.83177570093

 

SQL:

select employee_id , first_name , salary

from employees

where salary > (select avg(salary) from employees) //One row and one column can be compared with salary

 

//Query the employee information in the same department as the employee whose last name is 'King' (job ID, first name, salary, department id)

Ideas:

1). First query the department number where 'King' is located

select department_id

from employees

where last_name = 'King' //Department number: 80, 90

 

2). Inquire about the employee information of departments 80 and 90

select employee_id , first_name , salary , department_id

from employees

where department_id in (80,90);

SQL:

select employee_id , first_name , salary , department_id

from employees

where department_id in (select department_id cfrom employees where last_name = 'King'); //N行一列

 

//Query the information of the top 5 employees in the employee table by salary (number, name, salary)

Ideas:

1). First sort the salaries of all employees (sorted temporary table)

select employee_id , first_name , salary

from employees

order by salary desc

 

2). Then query the first 5 rows of employee information in the temporary table

select employee_id , first_name , salary

from (temporary table)

where rownum <= 5;

SQL: Merge

select employee_id , first_name , salary

from (select employee_id , first_name , salary from employees order by salary desc) //N行N列

where rownum <= 5;

 

Three: Paging query

//Perform paging query on the employee table in descending order of salary (10 pieces of data are displayed on each page, check the data on page 1 (1~10), and 11~20 on page 2)

Ideas:

1). Sort the data in the original table in descending order of salary.

select * from employees order by salary desc;

 

2). Add a column to the sorted table (add a column RN through rownum)

select t.* , rownum "RN"

from (select * from employees order by salary desc) t

 

3). The query result of the temporary table is filtered and the value of the rn column is between 11 and 20.

select *

from (temporary table with all original table information and row numbers)

where RN between 11 and 20;

SQL: Merge

select *

from (select t.* , rownum "RN" from

             (select * from employees order by salary desc) t)

where RN between 11 and 20;

 

Four: Set operator: Combine multiple query results into a temporary table.

I. Syntax: A query result set operator B query result.

II. Detailed explanation:

union: Union. (joint deduplication)

union all: union. (joint)

minus: difference set. (minus)

intersect: intersection. (poor)

III. Example:

//Query employee information of departments 60 and 70

select * from employees where department_id in (60,70);


//Query employee information of departments 70 and 90

select * from employees where department_id in (90,70);

 

The query results of the above two SQL statements are combined by the set operator.

 

//union: Union merges the query results of A and B, and keeps only one copy of the duplicate data.

select * from employees where department_id in (60,70)

union

select * from employees where department_id in (90,70); //Only one copy of employee information in department No. 70 is reserved.

 

//union all: The union merges the query results of A and B, and the duplicate data are displayed as numbers.

select * from employees where department_id in (60,70)

union all

select * from employees where department_id in (90,70); //How many copies of employee information in department 70 are kept

 

//minus: The difference set deletes the same result in A as in B.

select * from employees where department_id in (60,70)

minus

select * from employees where department_id in (90,70); //Delete duplicate data in A and B, department 70

 

AAAA//intersect: The intersection only keeps duplicate data in A and B.

select * from employees where department_id in (60,70)

intersect

select * from employees where department_id in (90,70); //Only keep the duplicate data of department 70

 

Note: Rules of use [Key points]: The query results can come from different tables and different columns. However, the number of columns must be the same, and the data types of the columns must be the same. The column names and data types in the final result depend on the first result set.

 

Five: table join query

I. Syntax: table1 join table2 on join condition

II. Detailed explanation:

Outer join:

Left outer join: main table left [outer] join from table

Right outer join: join main table from table right [outer]

Full outer join: main table full [outer] join main table

Inner join: from table inner join from table

Self-connection: the master table is connected to the slave table (the same as the master and slave)

III. Example:

//Query employee number, name, salary, department id, department name

Problem: When querying data from multiple tables, use table joins.

Idea: Combine multiple tables based on relational columns, splicing them into a large table, and then take some of the columns.

 

1). Left outer join [Key points] : table1 left join table2 on join condition (table1 is the main table)

//Query all employee information, and the corresponding department name (employees without a department are also in the query results)

select e.employee_id , e.first_name , e.salary , d.department_name

from employees e left join departments d on e.department_id = d.department_id;

 

2). Right outer join [understand]: table1 right join table2 on join condition (table2 is the main table)

//Query all department information, and all employee information in this department (departments without employees are also in the query results)

select e.employee_id , e.first_name , e.salary , d.department_name

from employees e right join departments d on e.department_id = d.department_id;

 

3). Full outer join [understand]: table1 full join table2 on join condition (both tables are main tables)

//Query all employee information and all department information (employees without departments and departments without employees are all in the query results)

select e.employee_id , e.first_name , e.salary , d.departmen0. t_name

from employees e FULL join departments d on e.department_id = d.department_id;

 

4). Inner join [understand]: table1 inner join table2 on join condition (both tables are slave tables)

//Query the information of all employees with departments (neither employees without departments nor departments without employees)

select e.employee_id , e.first_name , e.salary , d.department_name

from employees e inner join departments d on e.department_id = d.department_id;

 

5). Self-connection [Key points] : table1 connects table2 on connection conditions (two tables logically, the same table physically)

//Query employee number, name, direct leader (manager) id, direct leader's name

select e1.employee_id , e1.first_name , e1.manager_id , e2.first_name

from employees e1 left join employees e2 on e1.manager_id = e2.employee_id;

 

6). Three-table join query:

--Query all employee IDs, names, department names, and country IDs where the department is located

select * from employees e

inner join departments d

on e.department_id = d.department_id

inner join locations l

on d.location_id = l.location_id

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325400607&siteId=291194637