SELECT statement for SQL

What is a Select Statement for SQL?

SELECT statement for SQL, sometimes referred to as a Query, allows you to retrieve the records you want from the database so that you can manipulate or analyze them further. In this article, we’ll cover how to write a SELECT statement in SQL and look at some examples of how they are used. When writing your own SELECT statement for SQL, it’s important to consider two factors as mentioned below:

  • the source (where you are pulling your data from) and
  • the columns (which data fields you want).

These can both be changed using clauses in your statement.

What is Select Query

A select query is used to get data from a database. In simple terms, we use it when we need some information from our tables, for example, if we want to view all records of employees who were hired after December 2016 or if we want to see customers who have made more than 10 orders last year. In addition, using Select Query will help us when we need only specific columns from SQL table.

For example, let’s say that we have an Orders table with five columns: OrderDate, CustomerID, EmployeeID, ProductID, and Quantity. Now let’s assume that we only want to display records where EmployeeID = 2; thanks to Select Query we will be able to do just that without displaying other four fields.

SELECT * FROM Table

This is used to retrieve all of your data from your database. If you want to only get certain columns you would put SELECT columnname1, columnname2, etc. You could also select specific rows with ROW_NUMBER() OVER(ORDER BY) or specific criteria using WHERE. The SELECT statement for SQL provides data from one or more tables. You will see examples below on how these work with SELECT one statement for SQL:

SELECT statement for SQL
SELECT statement for SQL

In the above example FROM clause indicates which table you want to work with, and can include more than one table by using multiple table names through JOIN statement. The asterisk at the end of SELECT * indicates that you want all columns from all rows included in your result set. You also need to provide information about which column or columns you wish to see if you don’t want to fetch all columns.

Single value query using WHERE Clause

Consider you have a table called employee which consists of three columns such as id, name, and salary. The following query will display only one value i.e. salary of employee with Employee_id = 3:

SELECT emp_name, emp_salary FROM employee WHERE emp_id = 3; 

Here also we are selecting multiple SQL columns but fetching a single value using SQL WHERE clause.

The most common queries which uses ORDER BY keyword

For example, we have some query which reads: SELECT column1, column2 FROM table1 WHERE condition ORDER BY column3. In that case, ORDER BY specifies to retrieve only specified number of rows with order by particular columns(in ascending or descending order). We can use ASC and DESC where ASC means Ascending and DESC means Descending orders. If you want all rows then use SELECT * command.

The Most Common Queries Which Uses FETCH FIRST N ROWS ONLY Keyword

SELECT statement with FETCH FIRST N ROWS ONLY. In order to limit your result set from a SELECT query, you can use FETCH FIRST N ROWS ONLY keyword. This is most commonly used when we don’t want to fetch all rows from a SQL table and show them to users. The syntax of FETCH FIRST N ROWS ONLY clause in SELECT statement will be as follow:

SELECT * FROM EMPLOYEES
FETCH FIRST 3 ROWS ONLY;

The FETCH FIRST N ROWS ONLY clause must not be placed immediately after SELECT keyword but after ORDER BY, GROUP BY or HAVING.

You can read more on this from the below articles.

https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10002.htm

To read how to create a table click here. If you want to know how to insert into table then click here.

1 thought on “SELECT statement for SQL”

  1. Pingback: Pivot and Unpivot in SQL || How to convert rows to Columns in sql - DataWitzz

Leave a Comment

Scroll to Top