The six clauses of the select statement must be coded in the following order:

Structured Query Language (SQL) is a programming language that you use to manage data in relational databases. You can use SQL to create, read, update, and delete (CRUD) data in a relational database.

You can write SQL queries to insert data with INSERT, read data with SELECT, update with UPDATE, and delete data with DELETE.

This article will teach you how to write SQL SELECT queries. You'll learn the various ways you can write these queries, and what the expected results should be.

How to Use the SQL SELECT Statement

You can use the SQL SELECT statement to retrieve data from a database table that has been specified.

You can write your statement in various ways to get the exact data you want. These data are extracted from the database table and returned as a table.

// Syntax SELECT expression(s) FROM table(s) [WHERE condition(s)] [ORDER BY expression(s) [ ASC | DESC ]];

The preceding code is a very detailed syntax that encompasses a lot of information that I'll explain with examples.

Let's begin by going over the parameters and arguments:

  • expression(s): This represents the column(s) whose data you want to retrieve or the entire table's columns using an asterisk (*).
  • table(s): The name of the table(s) from which you want to retrieve records. The FROM clause must include at least one table.
  • WHERE condition(s): This is an optional field. This allows you to specify a condition that will guide the data that is retrieved for the specified column(s). If no conditions are specified, all records will be chosen.
  • ORDER BY expression(s): This is an optional field. This allows you to declare a column whose data will be used to sort the results. A comma should separate the values if you provide more than one expression.
  • ASC: This is an optional expression value. ASC sorts the result set by expression in ascending order. If this is not specified, this is the default behavior.
  • DESC: This is an optional expression value. DESC sorts the result set by expression in descending order.

SQL Select Queries

Suppose you have a database with the name "Users" and has the following data:

user_idfirst_namelast_nameagestatus1JohnDoe33Married2AliceTruss23Single3DavidBohle56Married4AaronBen34Single5LouisVic72Married6CharlesChris19Single

Let's now explore various queries and see how they work.

SQL Select All

You might need to select all the columns from a database. Instead of listing each column, you can use the asterisk (*) character.

SELECT * FROM Users;

Here is what your output will look like when you make use of this command on the user's table:

SQL Select Specified Columns

You can also fetch specified columns instead of all the columns by listing the columns and separating them by a comma:

SELECT first_name, last_name FROM Users;

Here is what your output will look like when you make use of this command on the user's table:

SQL Select WHERE Clause

You may want to return only rows that satisfy a specific condition. This condition can be specified using the optional SELECT * FROM Users; 1 clause. The SELECT * FROM Users; 1 clause allows you to retrieve records from a database table that match a given condition(s).

For example, suppose you only want to fetch users whose status is "single":

SELECT * FROM Users WHERE status = 'Single';

Here is what your output will look like when you make use of this command on the user's table:

Generally, the WHERE clause is used to filter the results. You can also use common operators like SELECT * FROM Users; 3, which you used, and others like SELECT * FROM Users; 4, SELECT * FROM Users; 5, SELECT * FROM Users; 6, SELECT * FROM Users; 7, SELECT * FROM Users; 8, SELECT * FROM Users; 9, and SELECT first_name, last_name FROM Users; 0.

SELECT Using Equality Operators

Suppose you want to fetch only users whose age is greater than 30. Then your query will be:

SELECT * FROM Users WHERE age > 30;

Here is what your output will look like when you make use of this command on the user's table:

You can also use other equality operators like SELECT * FROM Users; 4, SELECT * FROM Users; 6, and SELECT * FROM Users; 7.

SELECT Using the AND Operator

You might often want to use more than one condition to filter the table's contents. You can do this with the AND operator.

SELECT * FROM Users WHERE status = 'Single' AND age > 30;

Here is what your output will look like when you make use of this command on the user's table:

SELECT Using the BETWEEN Operator

You use the BETWEEN operator to get the range of data you want to filter. You can decide to use the equality and AND operator, but BETWEEN provides a better syntax.

SELECT * FROM Users WHERE age BETWEEN 20 AND 30;

Here is what your output will look like when you make use of this command on the user's table:

SELECT Using the IN Operator

Also, the SELECT first_name, last_name FROM Users; 0 operator lets you set more than one exact basis for filtering each row. For example, you can fetch only rows whose value is in the bracket defined:

SELECT * FROM Users WHERE age IN (56,33,10);

Here is what your output will look like when you make use of this command on the user's table:

SQL Select ORDER BY Clause

So far, you have learned how to fetch from your table with SQL, but you will notice that these data always follow the original order. You can adjust the order in which the data is fetched using the ORDER BY clause.

Two major options are Ascending (SELECT first_name, last_name FROM Users; 5) and descending (SELECT first_name, last_name FROM Users; 6) order. For example, you might want your table's rows to be arranged in ascending order based on the SELECT first_name, last_name FROM Users; 7:

SELECT * FROM Users ORDER BY first_name ASC;

Here is what your output will look like when you make use of this command on the user's table:

Note: You can always combine these options and clauses in one query to fetch exactly what you want.

Wrapping up!

In this article, you learned how to use the SQL SELECT query to retrieve data from a relational database. Other options are available, but these are the ones you'll most likely use regularly.

Have fun coding!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Joel Olawanle

Frontend Developer & Technical Writer

If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

When you code a select statement you must code the four main clauses in the following order?

C. The four main clauses used with a SELECT statement are: SELECT, FROM, WHERE, ORDER BY. The correct order of each clause is also important to note, as shown to give proper results when executed.

Which functions perform a calculation on the values of a column from selected rows?

Aggregate functions perform a calculation on a column of data and return a single value. Access provides a variety of aggregate functions, including Sum, Count, Avg (for computing averages), Min and Max. You sum data by adding the Sum function to your query, you count data by using the Count function, and so on.

Which function concatenates a string with the specified separator string added in between?

The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string.

Which operator can you use to test whether one or more rows are returned by a subquery?

The EXISTS Operator. The EXISTS operator check the number of rows returned by a subquery. If the subquery contains one or more rows, then the result is true and a row is placed in the result table; otherwise, the result is false and no row is added to the result table.

Toplist

Neuester Beitrag

Stichworte