SQL, Using The SELECT Statement
0 CommentsLast Updated on May 20, 2020 by jt
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:
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:
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:
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:
SELECT * FROM employees 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.