• We just launched and are currently in beta. Join us as we build and grow the community.

SQL SELECT Statement

amirpet

Quantum Hardware Engineer
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
66
Likes
63
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
The select command is used to retrieve data from the tables.

Consider the following table:

Users:

Firstname

Lastname

Age

Maritalstatus

Country

John

Smith

40

Married

Usa

Mathew

Simon

30

Married

UK

Bill

Steve

20

Single

Usa

Amanda

Rogers

28

Married

Germany

Steve

Hills

30

Single

france

Now to get all the data we simply query the database with the select command:

SELECT

*

FROM

Users

And the DBMS returns all set of rows in the user table.

The * in the select statement is to select all columns of the table, we can also specify certain columns that we wish to retrieve, all column titles must have to be separated by a comma.

If we want to list only the first name and last name of all users then:

SELECT

Firstname,

Lastname FROM

users

Firstname

Lastname

John

Smith

Mathew

Simon

Bill

Steve

Amanda

Rogers

Steve

Hills

We can also filter our choices and dataset on the conditions we specify.

The filtering of data set is made by the where clause, the where clause is used whenever we want some specific data that meets our selection criteria to shown.

Example: we want to list all the user that are from USA:

SELECT

*

FROM

Users WHERE

Country=

USA

Firstname

Lastname

Age

Maritalstatus

Country

John

Smith

40

Married

Usa

Bill

Steve

20

Single

Usa

Similarly we can also apply our data by filtering on numeric values. For example we want to get all the users who are above or equal to 30 years of age:

SELECT

*

FROM

Users WHERE

Age >=

30

Firstname

Lastname

Age

Maritalstatus

Country

John

Smith

40

Married

Usa

Mathew

Simon

30

Married

UK

Steve

Hills

30

Single

france

We can also specify multiple conditions in the where clause.

For example if we want to find the users living in USA and are married:

SELECT

*

FROM

Users WHERE

county=

’USA’ AND

Maritalstatus=

’Married’

Firstname

Lastname

Age

Maritalstatus

Country

John

Smith

40

Married

Usa

 

452,292

323,340

323,349

Top