SQL, Using The SELECT Statement

SQL, Using The SELECT Statement

0 Comments

Last 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_nobirth_datefirst_namelast_namegenderhire_date
100011953-09-02GeorgiFacelloM1986-06-26
100021964-06-02BezalelSimmelF1985-11-21
100031959-12-03PartoBamfordM1986-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_namelast_name
GeorgiFacello
BezalelSimmel
PartoBamford

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_nobirth_datefirst_namelast_namegenderhire_date
100011953-09-02GeorgiFacelloM1986-06-26
100031959-12-03PartoBamfordM196-08-28
100041954-05-01ChristianKoblickM1986-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_namelast_name
BasimAamodt
PeternelaAamodt
GrettaAamodt

 

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.

About SFG Contributor

Staff writer account for Spring Framework Guru

    You May Also Like

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.