Articles Catalogue
- Retrieval records
- Retrieve all rows and columns
- Screening rows
- Find rows that satisfy multiple query criteria
- Screening column
- Create meaningful column names
- Reference to alias columns in where clauses
- Values of tandem multiple columns
- Use conditional logic in select statements
- Restrict the number of rows returned
- Random return of rows
- Find null values
- Converting null values to actual values
- Find matches
- Ranking of query results
Retrieval records
Retrieve all rows and columns
-
Code
# All data in the query table select * from EMP; # Query specific columns select EMPNO, ENAME, JOB, SAL, MGR, HIREDATE, COMM, DEPTNO from EMP;
Screening rows
-
Code
# Filter rows to see rows that satisfy the criteria select * from EMP where DEPTNO = 10;
Find rows that satisfy multiple query criteria
-
Code
# Find rows that satisfy multiple criteria select * from EMP where DEPTNO = 10 or COMM is not null or SAL <= 2000 and DEPTNO = 20;
Screening column
-
Code
# Screening column select ENAME, DEPTNO, SAL from EMP;
Create meaningful column names
-
Code
# Create meaningful column names and use AS to create aliases select SAL as salary, COMM as commission from EMP;
Reference to alias columns in where clauses
-
error code
# Execution will report errors select SAL as salary, COMM as commission from EMP where salary < 5000;
-
Correct code, using embedded views
# aliases select * from (select SAL as salary, COMM as commission from EMP) x where salary < 5000;
-
Explain that the where clause will be executed before the select clause. In the case of failure, when the where clause is executed, salary and commission do not exist. The alias will not take effect until the where clause is executed. The from clause is queried before the where clause.
Values of tandem multiple columns
-
Find the value of the result
ENAME JOB CLARK MANAGER KING PRESIDENT MILLER CLERK -
The desired result
CLARK WORKS AS AMANAGER KING WORKS AS APRESIDENT MILLER WORKS AS ACLERK
-
Code
# Values of tandem multiple columns # CONCAT functions can concatenate values of multiple columns select concat(ENAME, ' WORKS AS A', JOB) as msg from EMP where DEPTNO = 10;
Use conditional logic in select statements
-
Code
# Use conditional logic in select statements select ENAME, SAL, case when SAL <= 2000 then 'UNDERPAID' when SAL >= 4000 then 'OVERPAID' else 'OK' end as status from EMP;
-
Result
ENAME SAL STATUS SMITH 800 UNDERPAID ALLEN 1600 UNDERPAID WARD 1250 UNDERPAID JONES 2975 OK MARTIN 1250 UNDERPAID BLAKE 2850 OK CLARK 2450 OK SCOTT 3000 OK KING 5000 OVERPAID TURNER 1500 UNDERPAID ADAMS 1100 UNDERPAID JAMES 950 UNDERPAID FORD 3000 OK MILLER 1300 UNDERPAID
Restrict the number of rows returned
-
Code
# Limit the number of rows returned select * from EMP limit 5;
Random return of rows
-
Code
# Random return of rows select ENAME, JOB from EMP order by rand() limit 5;
Find null values
-
Code
# Find null values select * from EMP where COMM is null;
Converting null values to actual values
-
Code
# Converting null values to actual values # The coalesce function returns the first non-null value in the parameter list. select coalesce(COMM, 0), ENAME from EMP;
# Use case select case when COMM is not null then COMM else 0 end from EMP;
Find matches
-
Code
# Query matches # '% I%'means that strings with I appearing anywhere will be retrieved, and'% ER' means that strings ending with ER will be retrieved. select ENAME, JOB from EMP where DEPTNO in (10, 20) and (ENAME like '%I%' or JOB like '%ER');
Ranking of query results
Returns query results in specified order
-
Code
# Sals grow up select ENAME, JOB, SAL from EMP where DEPTNO = 10 order by SAL asc;
Multi-field sorting
-
Code
# First in DEPTNO ascending order, then in SAL descending order select EMPNO, DEPTNO, SAL, ENAME, JOB from EMP order by DEPTNO, SAL desc;
Sort by clause
-
Code
# Result the search results according to the last two characters of the job field select ENAME, JOB from EMP order by substr(JOB, length(JOB) - 2);
null Value Processing in Sorting
-
Code
# null Value Processing in Sorting # Add auxiliary columns for sorting select ENAME, SAL, COMM from (select ENAME, SAL, COMM, case when COMM is null then 0 else 1 end as is_null from EMP) x order by is_null desc, COMM;
Dynamic adjustment of sorting items according to conditional logic
-
Code
# Dynamic adjustment of sorting items according to conditional logic select ENAME, SAL, JOB, COMM from EMP order by case when JOB = 'SALESMAN' then COMM else SAL end;