{{announcement.body}}
{{announcement.title}}

SQL: Using the SELECT Statement

DZone 's Guide to

SQL: Using the SELECT Statement

In this tutorial, learn the basics of using the SQL SELECT command.

· Database Zone ·
Free Resource

In this short tutorial, we’ll learn the basics of using the SQL SELECT command. To follow along, you will need to have MySQL installed on your system, and you will need to install this simple database.

Select All Columns From a Table

We use the “*” to select all the columns in a table. Here is an example:

SQL
 




xxxxxxxxxx
1


 
1
SELECT * FROM employees;


Output:

emp_no birth_date first_name last_name gender hire_date
10001 1953-09-02 Georgi Facello M 1986-06-26
10002 1964-06-02 Bezalel Simmel F 1985-11-21
10003 1959-12-03 Parto Bamford M 1986-08-28

Select Fewer Columns From a Table

We can select fewer columns from a table by naming the columns we want to return. Here is an example that only returns the first and last name from the employees’ table:

SQL
 




xxxxxxxxxx
1


 
1
SELECT first_name, last_name FROM employees;


Output:

first_name last_name
Georgi Facello
Bezalel Simmel
Parto Bamford

Count Number of Rows in a Table

To count the number of rows in a table, we use the Count function:

SQL
 




xxxxxxxxxx
1


 
1
SELECT COUNT(*) FROM employees;


Output:

Count(*)
300024

 

Select and Where Filter

We can filter the values that are returned by using the WHERE clause. Here is an example that only returns employees that are males:

SQL
 




xxxxxxxxxx
1


 
1
SELECT * FROM employees
2
where gender = 'M'


Output:

emp_no birth_date first_name last_name gender hire_date
10001 1953-09-02 Georgi Facello M 1986-06-26
10003 1959-12-03 Parto Bamford M 196-08-28
10004 1954-05-01 Christian Koblick M 1986-12-01

 

Ordering the Results

We can order the results base on the column value by using the ORDER BY statement. Here is an example that returns employees Order By last names:

Output:

first_name last_name
Basim Aamodt
Peternela Aamodt
Gretta Aamodt

 

Conclusion

In this short tutorial, you learned the basics of using the SQL SELECT statement. To learn more about SQL, please check out the MySQL course.

Topics:
database, select statement, sql, sql basics, tutorial

Published at DZone with permission of John Thompson , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}