SQL Queries, Today we see the most commonly used SQL queries in our daily developer’s life. If you are an SQL developer you must also go through these queries for revision purposes.
Define SELECT query or SELECT statement in SQL.
The SELECT statement is at the heart of most SQL queries. It defines what result set should be returned by the query, and is almost always used in conjunction with the FROM clause, which defines what part(s) of the database should be queried.
Using the wildcard character to SELECT all columns in a query:
Consider a database with the following two tables.
- Employee
| Id | FName | LName | DeptId |
| 1 | Jerry | Smith | 3 |
| 2 | Kamal | Sharma | 4 |
2. Departments
| Id | Name |
| 1 | Sales |
| 2 | Marketing |
| 3 | Finance |
| 4 | IT |
Simple select statement
* is the wildcard character used to select all available columns in a table. When used as a substitute for explicit column names, it returns all columns in all tables that a query is selecting FROM. This effect applies to all tables the query accesses through its JOIN clauses.
Consider the following query:
SELECT * FROM Employees
It will return all fields of all rows of the Employees table:
| Id | FName | LName | DeptId |
| 1 | Jerry | Smith | 3 |
| 2 | Kamal | Sharma | 4 |
Dot notation
To select all values from a specific table, the wildcard character can be applied to the table with dot notation.
Consider the following query:
SELECT
Employees.*,
Departments.Name
FROM
Employees
JOIN
Departments
ON Departments.Id = Employees.DeptIdThis will return a data set with all fields on the Employee table, followed by just the Name field in the Departments
table:
| Id | FName | LName | DeptId | Name |
| 1 | Jerry | Smith | 3 | Finance |
| 2 | Kamal | Sharma | 4 | IT |
Also Read: Most commonly used SQL Queries | Part-1
Leave a comment