SELECT Statement in Oracle

In this post, You will learn how to query data from a single table using the Oracle SELECT command.

Columns and rows make up tables in Oracle. Customer id, name, address, website, and credit limit, for example, are columns in the sample database’s customers table.

The SELECT statement, with the following syntax, is used to get data from one or more columns of a table:

SELECT
  column_1, 
  column_2, 
  ...
FROM
  table_name;

This SELECT statement has the following clauses:

  • To begin, type the name of the table from which you want to retrieve information.
    Second, specify the columns you want the data to be returned from. You must use a comma to separate each column if you have more than one (,).
  • It’s worth noting that the SELECT statement is quite complicated, with phrases like ORDER BY, GROUP BY, HAVING, and JOIN. To keep things simple, we’ll just look into the SELECT and FROM clauses in this article.

SELECT examples in Oracle

To further understand how the Oracle SELECT command works, let’s look at some instances.

use a single column to query data

The following statement is used to extract the client names from the customers table:

SELECT
  name
FROM
  customers;

Combining data from various columns in a query

You specify a list of comma-separated column names to query data from several columns.

The following example shows how to query data from the customer table’s customer id, name, and credit limit columns.

SELECT
    customer_id,
    name,
    credit_limit
FROM
    customers;

Querying data from all of a table’s columns

The following example pulls all rows of the customers table from all columns:

SELECT
    customer_id,
    name,
    address,
    website,
    credit_limit
FROM
    customers;

To make it easier, you may command Oracle to return data from all columns of a table using the shorthand asterisk (*) as follows:

SELECT * FROM customers;

When embedding the query in applications, you should never utilise the asterisk (). Even if you wish to retrieve data from all columns of a table, it is a good practise to specifically identify the columns from which you want to query data. The asterisk () shorthand should only be used for ad-hoc inquiries.

This is because, as the business changes, a table may have more or fewer columns in the future. If you utilise the asterisk (*) in your application code and assume that the table has a fixed set of columns, the application may either ignore the new columns or access the columns that have been removed.

%d bloggers like this: