SQL Select

HTML
CSS
C#
SQL

SQL Select Statement

The SELECT statement is the most commonly used command in Structured Query Language. It is used to access the records from one or more database tables and views. It also retrieves the selected data that follow the conditions we want. By using this command, we can also access the particular record from a particular column of the table. The table that stores the record returned by the SELECT statement is called a result-set table.

SELECT Column_Name_1, Column_Name_2, ….., Column_Name_N FROM Table_Name; 

In this SELECT syntax, Column_Name_1, Column_Name_2, ….., Column_Name_N are the name of those columns in the table whose data we want to read.

If you want to access all rows from all fields of the table, use the following SQL SELECT syntax with * asterisk sign:

SELECT * FROM table_name; 

The following SQL query displays all the values of each column from the above Student_records table:

SELECT * FROM Student_Records; 

The output of the above query is:

output of the above query

The following query displays the values of a particular column from the above Student_Record table:

SELECT Student_Id, Age, Percentage, Grade FROM Employee; 

Student Record table

SQL Where clause:

Explanation: The WHERE clause is used with the SELECT statement to return only those rows from the table, that satisfy the specified condition in the query. In SQL, the WHERE clause is not only used with SELECT, but it is also used with other SQL statements such as UPDATE, ALTER, and DELETE statements.

Example:

SELECT * FROM Name_of_Table WHERE [condition]; 

The following query shows the record of those employees from the above table whose Emp_Panelty is 500:

SELECT * FROM Employee_Details WHERE Emp_Panelty = 500; 

This SELECT query displays the following table in the result:

SELECT query displays the following table

SQL Group by clause:

Explanation: The GROUP BY clause is used with the SELECT statement to show the common data of the column from the table. The GROUP BY statement is often used with aggregate functions  (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result set by one or more columns.

Example:

SELECT column_Name_1, column_Name_2, ….., column_Name_N aggregate_function_name(column_Name2) FROM table_name GROUP BY column_Name1;

Car Details table:

Car Details table

The following SELECT with GROUP BY query lists the number of cars of the same price:

SELECT COUNT (Car_Name), Car_Price FROM Cars_Details GROUP BY Car_Price; 

The output of above GROUP BY query is shown below:

GROUP BY query

SQL HAVING clause:

Explanation: The HAVING clause in the SELECT statement creates a selection in those groups that are defined by the GROUP BY clause. The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.

Example:

SELECT column_Name_1, column_Name_2, ….., column_Name_N aggregate_function_name(column_Name_2) FROM table_name GROUP BY column_Name1 HAVING ;

Employee Having table:

employee table

The following query shows the total salary of those employees having more than 5000 from the above Employee_Having table:

SELECT SUM (Employee_Salary), Employee_City FROM Employee_Having GROUP BY Employee_City HAVING SUM(Employee_Salary)>5000; 

This HAVING query with SELECT statement shows the following table:

SQL Order by clause:

Explanation: The ORDER BY clause with the SQL SELECT statement shows the records or rows in a sorted manner. The ORDER BY clause arranges the values in both ascending and descending order. Few database systems arrange the values of column in ascending order by default.

Example:

SELECT Column_Name_1, Column_Name_2, ….., column_Name_N FROM table_name WHERE [Condition] ORDER BY[column_Name_1, column_Name_2, ….., column_Name_N asc | desc ];

Employee Order table:

Employee Order table

The following query sorts the salary of employees in descending order from the above Employee Order table:

SELECT * FROM Employee_Order ORDER BY Emp_Salary DESC; 

This SQL query shows the following table in the result:

SQL query

Course Video

NOTE: Practice below questions on site editor.

1. Write a SQL query to get all the columns data from the Production_Product table.

[OUTPUT: 100 ROWS]

2. Write a SQL query to get StoreId, name fromStore table.

[OUTPUT: 100 ROWS]

3. Write a SQL query to get all the data from employee table who’s gender is ‘F’ and MaritalStatus is ‘S’

[OUTPUT: 9 ROWS]

4. Write a SQL query to get productSubcategoryID and its count from Production.Product table.

[OUTPUT: 38 ROWS]
[NOTE: TRY TO SOLVE THIS IN SQL SERVER]

5. Write a SQL query to get ID, firstname, middlename and lastname of Person_Person based on LastName in descending order.

[OUTPUT: 100 ROWS]