Home Oracle Tutorials Structured Query Language Querying a Table



The action of retrieving data from a table of the database is called querying. Querying requires that data exists in the table. You should know the name of the table and what you wish to retrieve. What you wish to retrieve can be all the columns of a table or specific columns of the table.


Video Demonstration on Querying a Table : Part 1

querying a table

Tutorial demonstrating basic querying:

  • SELECT *
  • SELECT column(s) FROM table

 

The most basic form of the SQL statement is: (Syntax)

SELECT [*[, column, column,...]] FROM table_name;

Retrieving all the columns of a table:

To retrieve all the columns you can specify the asterisk (*) in the SELECT statement. To retreive all the data from the LOCATIONS table, the query would be :

SELECT * FROM LOCATIONS;

 

The query and the output as written SQL Developer is shown below:

 

Alternate method of retrieving all the data using SQL Developer:

  • Clear the SQL Worksheet window, by selecting the Clear button.
  • Select the LOCATIONS table from the main navigation tree by selecting on the name.
  • Select the SQL Worksheet tab from the right pane
  • Drag the LOCATIONS table from the main navigation tree to the SQL Worksheet window.
  • A SELECT statement will be created that would list all the columns of the LOCATIONS table
  • Execute the query, by selecting the Execute button.
  • The Results pane will display the rows that were retrieved.

Retrieving specific columns from a table:

To retrieve specific columns you must know the name of the table. If you are unsure of the column name, you can view the columns names using the DESCRIBE command in SQL*Plus or expand the table name in SQL Developer. Mention the column names in the SELECT statement one after another, separated by commas. The last column name on the list is not terminated with a a comma.

 

To retrieve the musician name, and number of albums for all music composers the query you would write is :

SELECT MUSICIAN_NAME, NUMBER_OF_ALBUMS

FROM COMPOSER;

Try the hands-on exercise on Querying the database, using our Sample Tables

This article is continued as : Querying a Table - Part 2

 
Bookmark and Share
We have 27 guests online