Introduction to SQL SELECT Query
Before jumping to discuss the SQL select query let’s begin with the SQL first. SQL stands for Structured Query Language. SQL is a query language that enables the ability to do various operations on the database or the database tables. There are various ways to interact with the database tables. The select command is nothing but it’s all about how we communicate with the database and its table for selecting the required records. This is a way of selecting the records from one or more tables. We can use the select query in many ways, few of them are easy and few are from the complex side. In this article, we will see the various uses of the select statements.
Syntax
Select is one of the basic commands of the relational database management system. We can use the SELECT keyword as a prefix to select the record(s) from a given table. The select query return set of records from the given one or more tables.
1. Selecting the required columns of a given table:
SELECT <column_A>, <column_B>, ...... FROM <My_table_name>;
Explanation:
SELECT is a command itself, we can use it to select the record from the table.
<column_A>, <column_B>, …… are the columns of the table My_table_name.
<My_table_name> is the name of a table.
2. Selecting all the columns of a table:
SELECT * FROM <My_table_name>;
Explanation:
SELECT is a command itself we can use to select the record from the table.
* denotes all the columns of the given table.
<My_table_name> is the name of a table.
3. Selecting the records with the WHERE clause:
SELECT <column_One>, <column_Two>, ...... FROM <My_table > WHERE <column_ One > = <Column_Value>;
Explanation:
<column_One>, <column_Two>, …… are the columns of the table My_table.
<My_table > is the name of a table.
WHERE is a keyword we can use in the SQL select statement to select the records by the specified condition.
4. Selecting the number of records:
SELECT COUNT(*) FROM <My_table>;
Explanation:
COUNT(*), this will give us the total number of rows of the table My_table.
<My_table > is the name of a table.
Examples of SQL SELECT Query
In this section, we will discuss some examples, considering the syntax mentioned above so that anyone can easily understand by putting little effort. Let’s practice some example to understand the SQL select statement better.
Suppose we have a database name “SCHOOL”. This database has tables as mentioned below:
Students
Class
1. SELECT with required columns example:
SELECT Student_ID, First_name, Last_name from Student;
Output:
SELECT Student_ID, Class from Class;
Output:
2. SELECT all the columns of a table example:
SELECT * from Student;
Output:
SELECT * from Class;
Output:
3. Selecting the records with WHERE clause example
SELECT * from Student WHERE First_name="Alex";
Output:
SELECT * from Class where Medium=' English ';
Output:
4. Selecting the number of records example:
SELECT COUN(*) as count from Student;
Output:
SELECT COUN(*) as count from Class;
Output:
Other Facts about the SQL SELECT
The select statement mentioned above can be performed on the relational database. There are various other select statement queries we can use to select the records. The select statement always returns a result set. This result set may contain zero (0), one or multiple records as well. There are various other things in the select statement we can use to get the desired result. We can use the JOIN keyword to select the records from two or more tables. There are various ways, we can use two or more select statements together to get the records from one or more tables. We should use a primary key to any table so that a record can be identified uniquely.
We can use the below mentioned optional clause with the SELECT statement:
- WHERE – we have already seen this with examples.
- GROUP BY – This is required before using the aggregate function.
- HAVING – We can perform an aggregate function using this over the GROUP BY statement.
- ORDER BY – We can use this with the SELECT to sort the order of the result set.
- AS – We have seen this while selecting the total records of a given table. This AS can be used to make an alias of either the selected column or the table.
Conclusion – SQL SELECT Query
We can use the SQL select statement to select the required columns or the records as per the business need. Almost every relational database has this SELECT command to select the record from the table. We can use select in various ways. We can select conditional based records. The select operation can be performed on one or more tables. We can combine various other commands with the SELECT statements. MySQL, ORACLE are examples of the relational database management system.
Recommended Articles
This is a guide to SQL SELECT Query. Here we discuss the introduction, syntax, examples and some other facts about the SQL Select Query. You may also look at the following articles to learn more –