|
Home >> FAQs/Tutorials >> Oracle DBA FAQ
Oracle DBA FAQ - Understanding SQL SELECT Query Statements
By: FYIcenter.com
Part:
1
2
3
4
5
6
7
8
A collection of 33 FAQs on Oracle SQL SELECT query statements. Clear answers are provided with tutorial exercises on selecting rows and columns from tables and views, sorting and counting query outputs, grouping outputs and applying group functions, joining tables, using subqueries.
Topics included in this FAQ are:
- What Is a SELECT Query Statement?
- How To Select All Columns of All Rows from a Table?
- How To Select Some Columns from a Table?
- How To Select Some Rows from a Table?
- How To Sort the Query Output?
- Can the Query Output Be Sorted by Multiple Columns?
- How To Sort Output in Descending Order?
- How To Use SELECT Statement to Count the Number of Rows?
- Can SELECT Statements Be Used on Views?
- How To Filter Out Duplications in the Returning Rows?
- What Are Group Functions?
- How To Use Group Functions in the SELECT Clause?
- Can Group Functions Be Mixed with Non-group Selection Fields?
- How To Divide Query Output into Groups?
- How To Apply Filtering Criteria at Group Level?
- How To Count Duplicated Values in a Column?
- Can Multiple Columns Be Used in GROUP BY?
- Can Group Functions Be Used in the ORDER BY Clause?
- How To Join Two Tables in a Single Query?
- How To Write a Query with an Inner Join?
- How To Define and Use Table Alias Names?
- How To Write a Query with a Left Outer Join?
- How To Write a Query with a Right Outer Join?
- How To Write a Query with a Full Outer Join?
- How To Write an Inner Join with the WHERE Clause?
- How To Write a Left Outer Join with the WHERE Clause?
- How To Name Query Output Columns?
- What Is a Subquery?
- How To Use Subqueries with the IN Operator?
- How To Use Subqueries with the EXISTS Operator?
- How To Use Subqueries in the FROM clause?
- How To Count Groups Returned with the GROUP BY Clause?
- How To Return Top 5 Rows?
Sample scripts used in this FAQ assumes that you are connected to the server with the HR user account
on the default database instance XE. See other FAQ collections on how to connect to the server.
Some sample scripts may require database tables created by other samples in the beginning of the collection.
What Is a SELECT Query Statement?
The SELECT statement is also called the query statement. It is the most frequently used
SQL statement in any database application. A SELECT statement allows you to retrieve data
from one or more tables, or views, with different selection criteria, grouping criteria and sorting orders.
How To Select All Columns of All Rows from a Table?
The simplest query statement is the one that selects all columns of all rows from a table:
"SELECT * FROM table_name;". The (*) in the SELECT clause tells the query to return all columns.
The tutorial exercise below gives you a good example:
SQL> SELECT * FROM departments;
DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID
------------- -------------------- ---------- -----------
10 Administration 200 1700
20 Marketing 201 1800
30 Purchasing 114 1700
40 Human Resources 203 2400
50 Shipping 121 1500
60 IT 103 1400
70 Public Relations 204 2700
80 Sales 145 2500
90 Executive 100 1700
......
How To Select Some Columns from a Table?
If you want explicitly tell the query to some columns, you can specify
the column names in SELECT clause. The following select statement
returns only two columns from the table "departments":
SQL> SELECT location_id, department_name FROM DEPARTMENTS;
LOCATION_ID DEPARTMENT_NAME
----------- ------------------------------
1700 Administration
1800 Marketing
1700 Purchasing
2400 Human Resources
1500 Shipping
1400 IT
2700 Public Relations
2500 Sales
1700 Executive
......
How To Select Some Rows from a Table?
If you don't want select all rows from a table, you can specify
a WHERE clause to tell the query to return only the rows that meets
the condition defined in the WHERE clause. The following select statement
only returns rows that has department name starts with the letter "C":
SQL> SELECT * FROM departments
2 WHERE department_name LIKE 'C%';
DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID
------------- -------------------- ---------- -----------
130 Corporate Tax 1700
140 Control And Credit 1700
180 Construction 1700
190 Contracting 1700
......
How To Sort the Query Output?
If you want the returning rows to be sorted, you can specify
a sorting expression in the ORDER BY clause. The following select statement
returns rows sorted by the values in the "manager_id" column:
SQL> SELECT * FROM departments ORDER BY manager_id;
DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID
------------- -------------------- ---------- -----------
90 Executive 100 1700
60 IT 103 1400
100 Finance 108 1700
30 Purchasing 114 1700
50 Shipping 121 1500
80 Sales 145 2500
10 Administration 200 1700
20 Marketing 201 1800
......
(Continued on next part...)
Part:
1
2
3
4
5
6
7
8
|