Overview: In this tutorial you will learn and understand the SELECT query for SQL explained with 4 different examples.
Select query for SQL
In SQL data are stored as a set of rows and columns which make a table like excel spreadsheet. Each rows represents a unique record and each columns represents a field in the table. If you want to see the data & records of SQL table the you need to use SELECT query in SQL.
The SELECT query for SQL used to return output in a table format. Either you can select few fields/columns or you can select all fields/columns from specific table.
First you need to use the database and then perform SELECT query for SQL to display the records and
Syntax 1:
To get output of few fields/columns, you can specify number of columns with their heading name in query as shown below SQL syntax.
SELECT
Coulmn1, Column2
FROM
Table_Name;
Syntax 2:
To display all fields/columns from specific table. In this case you can extract all columns of table without giving name of columns, only you need to use steric mark sign as “*“, and you will get the result/output of all columns available in specified table as shown below syntax.
SELECT *
FROM Table_Name;
Lets understand it more with few below examples of SELECT query use for “Customers_Tbl” table.
Example #1: Select command to extract 3 columns only
SELECT
Customer_ID, Customer_Name, Province
FROM
Customers_Tbl;
Result:
Example #2: SELECT command to retrieve all columns from Customers Table
You can see in below example we have use the steric mark “*” to get all columns available with “Customers_Tbl” table
SELECT *
FROM Customers_Tbl;
Output:
Example #3: SELECT query with Count function
SELECT query can also use for aggregated function to get the result. It will help to know, how many total numbers of rows we have in a specific table.
SELECT Count(*)
FROM Customers_Tbl;
Result:
Example #4: SELECT query with Group by (Group the data to exclude repetitive rows)
SELECT always work to display each rows, its never display you by grouping the rows if all rows have same values,
Example if you have 60 province in table and you want to see only one province with count of rows then you need to use GROUP BY clause with SELECT query as shown below SQL code.
SELECT
Province, Count(*)
FROM
Customers_Tbl
GROUP BY
Province;
Output:
Note:
Remember aggregated function can only be used on group by command.