Retrieving data with SQL queries: an introduction to the SELECT statement

Structured Query Language provides database users with a powerful and flexible mechanism for querying data – the SELECT statement. In this article, we’ll look at the general form of the SELECT statement and build some sample database queries. If this is your first introduction to the world of Structured Query Language, you may want to familiarize yourself with the basics of SQL before moving on. If you want to develop a new database from scratch, learning how to create databases and tables in SQL is a good place to start.

Now that you’ve covered the basics, let’s begin our exploration of the SELECT statement. As in previous SQL lessons, we will continue to use operators that conform to the ANSI SQL standard. You can check the documentation for your DBMS to determine if it supports advanced options that can improve the efficiency and/or efficiency of your SQL code.

General form of the SELECT statement

The general form of the SELECT statement is given below:

SELECT select_list
BY source
TRUE condition
GROUP ON expression em em>
IT HAS condition
TO ORDER expression

The first line of the statement tells the SQL processor that this statement is an instruction. CHOOSE and that we want to retrieve information from the database. select_list allows us to specify the type of information we want to receive. Sentence BY in the second line defines the specific table(s) of the database, and the clause TRUE allows us to limit the results to those records that match the specified condition(s). The last three paragraphs introduce advanced features beyond the scope of this article – we’ll cover them in future SQL articles.

The easiest way to learn SQL is to give an example. With that in mind, let’s take a look at some database queries. In this article, we use an employee table from a fictitious XYZ Corporation Human Resources database to illustrate all of our questions. Here’s the whole table:

Employee ID

td>

last name

td>

First name

td>

Income

td>

Reports to

td>
TR>

a

td>

blacksmith

td>

John

td>

32000

td>

2

td>
TR>

2

td>

scampis

td>

sue

td>

45000

td>

ZERO

td>
TR>

3

td>

Kendall

td>

Volume

td>

29500

td>

2

td>
TR>

4TD> Jones TD> Abraham TD> 35000TD> 2TD>
TR>
5TD> All TD> Bill TD> 17250TD> 4TD>
TR>
6TD> Reynolds TD> Allison TD> 1950TD> 4TD>
TR>
7TD> Johnson TD> Katie TD> 21000TD> 3TD>
TR>
table>

Get the whole table

The Human Resources Director of XYZ Corporation receives a monthly report with salary and reporting information for every employee in the company. The generation of this report is an example of the simplest form of the SELECT statement. It simply extracts all the information in the database table – every column and every row. Here is the query that will output this result:

 ВЫБРАТЬ * 
ОТ сотрудников

Pretty easy, right? Star appear in select_list

, is a wildcard used to inform the database that we want information from all columns in the employee table specified in the FROM clause. We wanted to get all the information in the database, so there was no need to use a WHERE clause to restrict the selected rows from the table. This is what our query results look like: Employee ID TD> Surname TD> First name TD> Salary TD>
Reports to TD>
TR> ———-TD> ——– TD> ——— TD> —— TD>
——— TD>
TR> 1TD> Smith TD> John TD> 32000TD>
2TD>
TR> 2TD> Scampi TD> Sue TD> 45000TD>
NULL TD>
TR> 3TD> Kendall TD> Tom TD> 29500TD>
2TD>
TR> 4TD> Jones TD> Abraham TD> 35000TD>
2TD>
TR> 5TD> All TD> Bill TD> 17250TD>
4TD>
TR> 6TD> Reynolds TD> Allison TD> 1950TD>
4TD>
TR> 7TD> Johnson TD> Katie TD> 21000TD>
3TD>
TR>

table>


Rate the article

solutics.ru Add a comment

















© 2022 solutions.ru | Contacts | Privacy Policy

Leave a Reply

Your email address will not be published.