Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

SQL SELECT statement

17 July 2020

SQL SELECT statement

The SQL SELECT operator is used to extract records from one or more tables in a SQL database.

Syntax of the SELECT operator

SELECTexpressions_id
FROM tabs
[WHERE conds]

Parameters or arguments

  • expressions_id – fields or calculations that you want to get.
  • tabs – tables from which you want to upload data. After FROM operator at least one table must be specified.
  • WHERE conds – Optional. Conditions that must be met for the records to be selected. If no conditions are specified, all records will be selected.

SELECTING ALL FIELDS FROM ONE TABLE

Consider an example showing how to use the SQL SELECT operator that selects all fields from a table.

SELECT *
FROM suppls
WHERE city_id = 'London'.
ORDER BY city_id DESC;

In this example of SQL SELECT statement, we used * to view all fields from the suppliers table where the suppliers live in London city. The resulting set is sorted by city in descending order.

SELECT INDIVIDUAL FIELDS FROM THE SAME TABLE

You can also use the SQL SELECT statement to select individual fields from a table.

SELECT suppl_name,
city_id,
state_id
FROM suppls
WHERE suppl_id > 1000
ORDER BY suppl_name ASC, city_id DESC;

This SQL SELECT example will only return suppl_name, city_id, state_id fields from a suppls table where the suppl_id value exceeds 1000. The results are sorted by suppl_name in ascending order, and city in descending order.

SELECTION OF FIELDS FROM SEVERAL TABLES

You can also use the SQL SELECT statement to extract fields from multiple tables.

SELECT ords.order_id,
suppls.suppl_name
FROM suppls
INNER JOIN ords
ON suppls.suppl_id = ords.suppl_id
ORDER BY ord_id;

This SQL SELECT example connects two tables into the resulting set, showing the order_id and suppl_name fields where the suppl_id value is in both the suppls and order tables. The results are sorted by order_id in ascending order.

Read more about SQL joins

Practical Exercise #1

Based on the employee table below, select all fields whose salary is less than or equal to $ 52,500 (sorting is not required):

--create table empls
CREATE TABLE empls
( empl_number int NOT NULL,
empl_name char(50) NOT NULL,
salary_id int,
CONSTRAINT empls_pk PRIMARY KEY (empl_number)
);

-- insert entries in the empls table.
insert into empls values (1, 'Kuzjakin', 3500);
insert into empls values (2, 'Bakini',7200);
insert into empls values (3, 'Galingen',51000);
insert into empls values (4, 'Medajan',68000);
insert into empls values (5, 'Simkhan',85200);

Contents of the empls table:

empl_numberempl_namesalary_id
1Kuzjakin3500
2Bakini7200
3Galingen51000
4Medajan68000
5Simkhan85200

 

Solution for Practical Exercise #1:
The next SQL SELECT statement will select records from the empls table:

SELECT *
FROM empls
WHERE salary_id <= 52500;

As a result of sampling we’ll get:

empl_numberempl_namesalary_id
1Kuzjakin3500
2Bakini7200
3Galingen51000

 

Practical Exercise #2
Based on the table below, select the unique city_id values that are in Florida and organize the results in descending order by city_id:

--create a suppls table
CREATE TABLE suppls
( suppl_id int NOT NULL,
suppl_name char(50) NOT NULL,
city_id char(50),
state_id char(25),
CONSTRAINT suppls_pk PRIMARY KEY (suppl_id)
);
-- insert entries in the suppls table.
insert into suppls values (1, 'Mari', 'Houston', 'Texas');
insert into suppls values (2, 'Frida', 'Melbourne', 'Florida');
insert into suppls values (3, 'Madlen', 'Phoenix', 'Arizona');
insert into suppls values (4, 'Valentina', 'San Diego', 'California');
insert into suppls values (5, "Amba", "Jacksonville", "Florida");

Contents of the suppls table:

suppl_idsuppl_namecity_idstate_id
1MariHoustonTexas
2FridaPhiladelphiaPennsylvania
3MadlenPhoenixArizona
4ValentinaSanDiegoCalifornia
5AmbaJacksonvilleFlorida

 

Solution for Practical Exercise #2:
The following SQL SELECT operator selects records from the suppls table:

SELECT DISTINCT city_id
FROM suppls
WHERE state_id = 'Florida'.
ORDER BY city_id DESC;

The result of the sample will be:

city_id
Jacksonville
Melbourne

 

Practical Exercise #3
Based on the suppliers and orders tables below, select the suppl_id and suppl_name fields from the suppls table, and select the order_date field from the ords table, where the suppl_id field value in the suppliers table matches the suppl_id field value in the orders table. Sort the results by suppl_id in descending order.

--create the suppls table
CREATE TABLE suppls
( suppl_id int NOT NULL,
suppl_name char(50) NOT NULL,
city_id char(50),
state_id char(25),
CONSTRAINT suppls_pk PRIMARY KEY (suppl_id)
);
-- insert entries in the suppls table.
insert into suppls values (1, 'Mari', 'Houston', 'Texas');
insert into suppls values (2, 'Frida', 'Melbourne', 'Florida');
insert into suppls values (3, 'Madlen', 'Phoenix', 'Arizona');
insert into suppls values (4, 'Valentina', 'San Diego', 'California');
insert into suppls values (5, "Amba", "Jacksonville", "Florida");

Contents of the suppls table:

suppl_idsuppl_namecity_idstate_id
1MariHoustonTexas
2FridaPhiladelphiaPennsylvania
3MadlenPhoenixArizona
4ValentinaSanDiegoCalifornia
5AmbaJacksonvilleFlorida

 

--create an ords table
CREATE TABLE ords
( order_id int NOT NULL,
suppl_id int NOT NULL,
order_date date NOT NULL,
quantity int,
CONSTRAINT ords_pk PRIMARY KEY (order_id)
);
-- insert entries in the table of orders
insert into ords values (1.1, '05.05.2014', 100);
insert into ords values (2,3,'12.02.2015',300);
insert into ords values (3,5,'12.01.2016',500);

Contents of the table:

suppl_idsuppl_nameord_date
5Amba12.01.2016
3Madlen12.02.2015
1Mari05.05.2014

 

SELECT statement in SQL Server

 
Tags: , , , , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...