Home >> FAQs/Tutorials >> Oracle DBA FAQ >> Index

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:

  1. What Is a SELECT Query Statement?
  2. How To Select All Columns of All Rows from a Table?
  3. How To Select Some Columns from a Table?
  4. How To Select Some Rows from a Table?
  5. How To Sort the Query Output?
  6. Can the Query Output Be Sorted by Multiple Columns?
  7. How To Sort Output in Descending Order?
  8. How To Use SELECT Statement to Count the Number of Rows?
  9. Can SELECT Statements Be Used on Views?
  10. How To Filter Out Duplications in the Returning Rows?
  11. What Are Group Functions?
  12. How To Use Group Functions in the SELECT Clause?
  13. Can Group Functions Be Mixed with Non-group Selection Fields?
  14. How To Divide Query Output into Groups?
  15. How To Apply Filtering Criteria at Group Level?
  16. How To Count Duplicated Values in a Column?
  17. Can Multiple Columns Be Used in GROUP BY?
  18. Can Group Functions Be Used in the ORDER BY Clause?
  19. How To Join Two Tables in a Single Query?
  20. How To Write a Query with an Inner Join?
  21. How To Define and Use Table Alias Names?
  22. How To Write a Query with a Left Outer Join?
  23. How To Write a Query with a Right Outer Join?
  24. How To Write a Query with a Full Outer Join?
  25. How To Write an Inner Join with the WHERE Clause?
  26. How To Write a Left Outer Join with the WHERE Clause?
  27. How To Name Query Output Columns?
  28. What Is a Subquery?
  29. How To Use Subqueries with the IN Operator?
  30. How To Use Subqueries with the EXISTS Operator?
  31. How To Use Subqueries in the FROM clause?
  32. How To Count Groups Returned with the GROUP BY Clause?
  33. 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 


Selected Developer Jobs:

More...