Site icon Software Testing Class

SQL SELECT Query

Sql select statement

The SQL SELECT Query the basic query is used to retrieve the selected data from database. This is very powerful and largely used command. The result of the select query is stored in result table which is also called as result-set

It’s part of the data manipulation language that is responsible for query the data from the database.

SQL SELECT Query statement syntax

It is the most frequently used SQL command and has the following general syntax

SELECT column_name(s) FROM table_name

Or

SELECT * FROM table_name

Examples:
Let’s see upon executing following query on Employee table resulting following list:

SELECT * FROM Employee

Following is the result after executing the above SQL query:

Employee_Id Full_Name Gender Date_Of_Birth Phone
1 Tanya Smith Female 1982-09-30 9123456789
2 Janet Tampi Female 1980-07-10 9876543210
3 Nilsen Phil Male NULL 9112233445
4 Nilsen Jones Male 1983-09-17 9988776655
5 Pettersen Willams Male 1980-07-12 9944552299

Note: In SQL all queries are case insensitive, so we can use command SELECT or select.

In above query we have seen how to get all the rows and columns from the Employee table.

Using the SELECT keyword allows you to select all columns or specific column(s). The ‘*’ is used to select all columns in the result set & if you want to any specific column(s) in the result set then you can specify comma separated column name list after the SELECT keyword like the one shown below:

SELECT Full_name, Date_Of_Birth, Phone FROM Employee

Following is the result after executing the above SQL query:

Full_Name Date_Of_Birth Phone
Tanya Smith 1982-09-30 9123456789
Janet Tampi 1980-07-10 9876543210
Nilsen Phil NULL 9112233445
Nilsen Jones 1983-09-17 9988776655
Pettersen Willams 1980-07-12 9944552299

SQL Concat Function:

SQL query also supports the functions to join the column data & display in one column. The Concat () function is used in the SELECT query to join the multiple column data.

If you are looking for Employee Name with Gender in the format of [Full_Name, (Gender)] then Concat () function will help you to retrieve the required data in the above format using following query:

SELECT Concat(Full_Name, '(', Gender, ')'), Phone FROM Employee

In this query:

Following is the result after executing the above SQL query:

Phone
Tanya Smith (Female) 9123456789
Janet Tampi (Female) 9876543210
Nilsen Phil (Male) 9112233445
Nilsen Jones (Male) 9988776655
Pettersen Willams (Male) 9944552299

SQL Alias:

When we use the any function in the SQL query and execute the query then in the result set name of the column is showing weird. Here while using any function in the query we can use Alias name so that in the result set name of the column is appears as the Alias name what you entered.

SELECT Concat(Full_Name, '(', Gender, ')') [AS] Name(M/F), Phone FROM Employee

Following is the result after executing the above SQL query:

Name (M/F) Phone
Tanya Smith (Female) 9123456789
Janet Tampi (Female) 9876543210
Nilsen Phil (Male) 9112233445
Nilsen Jones (Male) 9988776655
Pettersen Willams (Male) 9944552299

Such as Concat() function SQL support lots of function, Here you can get list of most popular function used regularly in testing:

SQL Aggregate Functions:

SQL Scalar Functions:

Over to You!!

Please let me us know your thoughts and feedback through comments. All you experienced DB testers out there, please add your experiences to our article.

If you like this article, consider subscribing to get all Testing Updates in your Inbox for FREE enter Email here:

Enter your email address:
Exit mobile version