SELECT statement syntax and examples | TechTarget (2024)

Feature

In this chapter, learn how to use the SELECT statement with SELECT statement examples, including the basics of the SELECT statement syntax.

Published: 19 Aug 2009

SELECT statement syntax and examples | TechTarget (1)

An introduction to the SELECT statement

To help you learn to code the SELECT statement, this chapter starts by presenting its basic syntax. Next, it presents several examples that will give you an idea of what you can do with this statement. Then, the rest of this chapter will teach you the details of coding this statement.

The basic syntax of the SELECT statement Figure 3-1 presents the basic syntax of the SELECT statement. The syntax summary at the top of this figure uses some conventions that are used throughout this book. First, capitalized words are called keywords, and you must spell them exactly as shown, though you can use whatever capitalization you prefer. For example, the following are equivalent: "SELECT", "Select", "select" and "sELeCt". Second, you must provide replacements for words in lowercase. For example, you must enter a list of columns in place of select_list, and you must enter a table name in place of table_source.

Beyond that, you can choose between the items in a syntax summary that are separated by pipes (|) and enclosed in braces ({}) or brackets ([]). And you can omit items enclosed in brackets. If you have a choice between two or more optional items, the default item is underlined. And if an element can be coded multiple times in a statement, it's followed by an ellipsis (…). You'll see examples of pipes, braces, default values, and ellipses in syntax summaries later in this chapter. For now, compare the syntax in this figure with the coding examples in figure 3-2 to see how the two are related.

The syntax summary in this figure has been simplified so that you can focus on the four main clauses of the SELECT statement: the SELECT clause, the FROM clause, the WHERE clause, and the ORDER BY clause. Most of the SELECT statements you code will contain all four clauses. However, only the SELECT and FROM clauses are required.

The SELECT clause is always the first clause in a SELECT statement. It identifies the columns you want to include in the result set. These columns are retrieved from the base tables named in the FROM clause. Since this chapter focuses on retrieving data from a single table, the FROM clauses in all of the statements in this chapter name a single base table. In the next chapter, though, you'll learn how to retrieve data from two or more tables. And as you progress through this book, you'll learn how to select data from other sources such as views and expressions.

The WHERE and ORDER BY clauses are optional. The ORDER BY clause determines how the rows in the result set are to be sorted, and the WHERE clause determines which rows in the base table are to be included in the result set. The WHERE clause specifies a search condition that's used to filter the rows in the base table. This search condition can consist of one or more Boolean expressions, or predicates. A Boolean expression is an expression that evaluates to True or False. When the search condition evaluates to True, the row is included in the result set.

In this book, we won't use the terms "Boolean expression" or "predicate" because they don't clearly describe the content of the WHERE clause. Instead, we'll just use the term "search condition" to refer to an expression that evaluates to True or False.

The simplified syntax of the SELECT statement SELECT select_list FROM table_source [WHERE search_condition] [ORDER BY order_by_list]

The four clauses of the SELECT statement

ClauseDescription
SELECTDescribes the columns that will be included in the result set.
FROMNames the table from which the query will retrieve the data.
WHERESpecifies the conditions that must be met for a row to be included in the result set. This clause is optional.
ORDER BYSpecifies how the rows in the result set will be sorted. This clause is optional.

Description

  • You use the basic SELECT statement shown above to retrieve the columns specified in the SELECT clause from the base table specified in the FROM clause and store them in a result set.
  • The WHERE clause is used to filter the rows in the base table so that only those rows that match the search condition are included in the result set. If you omit the WHERE clause, all of the rows in the base table are included.
  • The search condition of a WHERE clause consists of one or more Boolean expressions, or predicates, that result in a value of True, False, or Unknown. If the combination of all the expressions is True, the row being tested is included in the result set. Otherwise, it's not.
  • If you include the ORDER BY clause, the rows in the result set are sorted in the specified sequence. Otherwise, the sequence of the rows is not guaranteed by Oracle. Note
  • The syntax shown above does not include all of the clauses of the SELECT statement. You'll learn about the other clauses later in this book.

Note

  • The syntax shown above does not include all of the clauses of the SELECT statement. You'll learn about the other clauses later in this book.

SELECT statement examples

Figure 3-2 presents five SELECT statement examples. All of these statements retrieve data from the Invoices table.

The first statement in this figure retrieves all of the rows and columns from the Invoices table. This statement uses an asterisk (*) as a shorthand to indicate that all of the columns should be retrieved, and the WHERE clause is omitted so there are no conditions on the rows that are retrieved. You can see the results after this statement as they're displayed by SQL Developer. Here, both horizontal and vertical scroll bars are displayed, indicating that the result set contains more rows and columns than can be displayed on the screen at one time. Notice that this statement doesn't include an ORDER BY clause. Without an ORDER BY clause, Oracle doesn't guarantee the sequence in which the rows are presented. They might be in the sequence you expect, or they might not. As a result, if the sequence matters to you, you should include an ORDER BY clause.

The second statement retrieves selected columns from the Invoices table. As you can see, the columns to be retrieved are listed in the SELECT clause. Like the first statement, this statement doesn't include a WHERE clause, so all the rows are retrieved. Then, the ORDER BY clause causes the rows to be sorted by the invoice_total column in ascending sequence. Later in this chapter, you'll learn how to sort rows in descending sequence.

The third statement also lists the columns to be retrieved. In this case, though, the last column is calculated from two columns in the base table (credit_total and payment_total), and the resulting column is given the name total_credits. In addition, the WHERE clause specifies that only the invoice with an invoice_id of 17 should be retrieved.

The fourth SELECT statement includes a WHERE clause whose condition specifies a range of values. In this case, only invoices with invoice dates between May 1, 2008 and May 31, 2008 are retrieved. In addition, the rows in the result set are sorted by invoice date.

The last statement in this figure shows another variation of the WHERE clause. In this case, only those rows with an invoice_total greater than 50,000 are retrieved. Since none of the rows in the Invoices table satisfies this condition, the result set is empty.

A SELECT statement that retrieves all the data from the Invoices table

 SELECT * FROM invoices

SELECT statement syntax and examples | TechTarget (2)
(114 rows selected)

A SELECT statement that retrieves three columns from each row, sorted in ascending sequence by invoice_total

 SELECT invoice_number, invoice_date, invoice_total FROM invoices ORDER BY invoice_total

SELECT statement syntax and examples | TechTarget (3)
(114 rows selected)

A SELECT statement that retrieves two columns and a calculated value for a specific invoice

 SELECT invoice_id, invoice_total, (credit_total + payment_total) AS total_credits FROM invoices WHERE invoice_id = 17

SELECT statement syntax and examples | TechTarget (4)

A SELECT statement that retrieves all invoices between given dates

 SELECT invoice_number, invoice_date, invoice_total FROM invoices WHERE invoice_date BETWEEN '01-MAY-2008' AND '31-MAY-2008' ORDER BY invoice_date

SELECT statement syntax and examples | TechTarget (5)
(70 rows selected)

A SELECT statement that returns an empty result set

 SELECT invoice_number, invoice_date, invoice_total FROM invoices WHERE invoice_total > 50000

SELECT statement syntax and examples | TechTarget (6)

Figure 3-2 SELECT statement examples

Copyright info

This chapter is excerpted from the book, Murach's Oracle SQL and PL/SQL, authored by Joel Murach, published by Mike Murach & Associates, Inc., August, 2008. ISBN: 978-1-890774-50-9.

Dig Deeper on Oracle development languages

  • T-SQL (Transact-SQL)By: AdamHughes
  • Oracle LEFT JOIN vs. LEFT OUTER JOIN: What's the difference?By: LindsayMoore
  • Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQLBy: JohnViescas
  • What's the difference between DDL and DML?By: BridgetBotelho
SELECT statement syntax and examples | TechTarget (2024)

FAQs

What is the syntax of SELECT statement? ›

SELECT statements

The syntax is: SELECT column1, column2 FROM table1, table2 WHERE column2='value'; In the above SQL statement: The SELECT clause specifies one or more columns to be retrieved; to specify multiple columns, use a comma and a space between column names.

What are the 6 clauses of SELECT statement? ›

SQL SELECT has different clauses to manage the data output. They are: FROM , AS , GROUP BY , HAVING , INTO , ORDER BY , * (asterisk). Let's see how we can use each clause within the SELECT syntax.

How to write a SELECT query? ›

The SQL SELECT Statement
  1. SELECT column1, column2, columnN FROM table_name;
  2. SELECT * FROM table_name;
  3. CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
  4. SELECT ID, NAME, SALARY FROM CUSTOMERS;
  5. SELECT * FROM CUSTOMERS;

How to use SELECT statement in SQL function? ›

Syntax. FROM table_name; Here, column1, column2, ... are the field names of the table you want to select data from. The table_name represents the name of the table you want to select data from.

What is the basic syntax for a SELECT statement in MySQL? ›

The SELECT … INTO statement serves to create new tables and populate tables with data. The standard syntax of this MySQL statement is as follows: SELECT column1, column2, column3, ...

Which of the following is the proper syntax for a simple SELECT statement? ›

Basic SELECT statement

The basic syntax for a SELECT statement is presented below. SELECT [DISTINCT | ALL] {* | select_list} FROM {table_name [alias] | view_name} [{table_name [alias] | view_name}]...

What is the structure of SELECT statement? ›

SELECT is the most complex statement in SQL, with optional keywords and clauses that include: The FROM clause, which indicates the table(s) to retrieve data from. The FROM clause can include optional JOIN subclauses to specify the rules for joining tables.

What are the 10 examples of clauses? ›

Give some examples of clauses.
  • As soon as I reach the office (dependent or subordinate clause)
  • I did not bring my umbrella. ( independent clause)
  • When the little boy saw his mom (dependent or subordinate clause)
  • Collect your parcel from the courier office. ( ...
  • Though we left home early (dependent or subordinate clause)

What are the 5 major clauses of the SELECT command? ›

The Five Clauses of the SELECT statement
  • SELECT – the columns in the result set.
  • FROM – names the base table(s) from which results will be retrieved.
  • WHERE – specifies any conditions for the results set (filter)
  • ORDER BY – sets how the result set will be ordered.
  • LIMIT – sets the number of rows to be returned.

What is a simple SELECT query? ›

A select query is a database object that shows information in Datasheet view. A query does not store data, it displays data that is stored in tables. A query can show data from one or more tables, from other queries, or from a combination of the two.

What symbol is used for SELECT statements? ›

Answer: The star symbol is used to select the statement.

What is the syntax of SELECT count? ›

Here is the basic syntax: SELECT COUNT(column_name) FROM table_name; The SELECT statement in SQL tells the computer to get data from the table. COUNT(column_name) will not include NULL values as part of the count.

How to format a SELECT statement in SQL? ›

Select Edit -> SQL Formatter -> Format Current Query (or press F12). Only the current query would be formatted. -- Format Selected Query: To format a selected query(s) in set of query(s), select the query(s) to be formatted. Select Edit -> SQL Formatter -> Format Selected Query (or press Ctrl+F12).

How to write 2 SELECT statements in SQL? ›

Procedure
  1. To combine two or more SELECT statements to form a single result table, use the set operators: UNION, EXCEPT or INTERSECT. ...
  2. To keep all duplicate rows when combining result tables, specify the ALL keyword with the set operator clause.

What is the syntax to SELECT a database? ›

Syntax. USE DatabaseName; Here, the DatabaseName is the name of the database that we want to select. The database name is always unique within the RDBMS.

What is the syntax of SELECT count query? ›

In SQL, you can make a database query and use the COUNT function to get the number of rows for a particular group in the table. Here is the basic syntax: SELECT COUNT(column_name) FROM table_name; COUNT(column_name) will not include NULL values as part of the count.

Which of the following is the correct syntax for SELECT into statement? ›

SELECT INTO Syntax

SELECT column1, column2, column3, ... WHERE condition; The new table will be created with the column-names and types as defined in the old table. You can create new column names using the AS clause.

What is acceptable syntax for the SELECT keyword? ›

In database management, the SELECT keyword is typically used for selecting data from a database. In MySQL, the acceptable syntax for the SELECT keyword include: SELECT column1, column2, .....

Top Articles
Latest Posts
Article information

Author: Sen. Emmett Berge

Last Updated:

Views: 5708

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Sen. Emmett Berge

Birthday: 1993-06-17

Address: 787 Elvis Divide, Port Brice, OH 24507-6802

Phone: +9779049645255

Job: Senior Healthcare Specialist

Hobby: Cycling, Model building, Kitesurfing, Origami, Lapidary, Dance, Basketball

Introduction: My name is Sen. Emmett Berge, I am a funny, vast, charming, courageous, enthusiastic, jolly, famous person who loves writing and wants to share my knowledge and understanding with you.