Previous | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Next |
SQL SELECT Statement
SELECT statement is the most commonly used command in SQL. The SELECT statement allows the user to retrieve records/ data or do a query in a table from the database. The query may retrieve information from column or columns from a table. To create a SELECT Statement the column(s) name must be specified and also the table name from which they have to retrieved.
SQL SELECT Statement Syntax
SELECT column1, column2 from tablename
[WHERE condition]
[GROUP BY condition]
[HAVING condition]
[ORDER BY condition];
tablename is the name of the table from which the data is retrieved.- column1, column2 are the names of the columns from which data is retrieved.
- The codes in the brackets are optional.
Conditional selections used in the where clause
= Equal
> Greater Than
<>= Greater Than or Equal To
<= Less Than or Equal To <> Not Equal
Sample Database Table - EmployeeDetails
UserId | EmailAddress | FirstName | LastName | Age | Salary | City |
---|---|---|---|---|---|---|
1 | ravi@123.com | Ravi | Kumar | 20 | 20,000 | Bangalore |
2 | kiran12@12343.com | kirankani | paul | 25 | 35,000 | Mumbai |
3 | rahul@123.com | Rahul | Patil | 28 | 25,000 | Pune |
4 | mirza@123 | Sania | Mirza | 23 | 50,000 | Hyderabad |
5 | shoaib@123.com | shoaib | Malik | 28 | 50,000 | Hyderabad |
6 | prisha@123.com | Prisha | Ruth | 21 | 21,000 | Delhi |
Please Note: The table mentioned is for better understanding and for explanation purpose but the real tables may contain more or less columns. All the examples are based on the table mentioned above.
Example 1
Let us start learning by selecting all fields from the table EmployeeDetails
SELECT * from EmployeeDetails;
When this query is run it will display all the columns of the table.
Example 2: If you want to retrieve data for some fields from the table than selecting all fields using the “*” in select command. You can do this way. S
SELECT EmailAddress, FirstName from EmployeeDetails;
This select statement would return all Email Address and First Names from the EmployeeDetails Table.
You can also use the clauses with SELECT statement like WHERE, GROUP BY, HAVING, ORDER BY. We will be discussing them at a later stage.
Please Note: It is not mandatory to use the WHERE, GROUP BY, HAVING, ORDER BY with the SELECT statement but in a SELECT Statement only SELECT and FROM are mandatory.
Using expressions in an SQL SELECT Statement
As discussed earlier we can use arithmetic operators in the expressions in the SELECT statement using the WHERE, HAVING, ORDER BY clauses. The operators are executed in a defined and set order of precedence where there is more than one arithmetic operator in the statement.
The order of evaluation of Arithmetic operators in an expression is parentheses, division, multiplication, addition, and subtraction. The evaluation starts from the left to the right of the expression.
Example 3
If you want to list out all the employee details whose salary is greater than 30,000 then you will write the query like this.
SELECT * from EmployeeDetails WHERE Salary >30,000;
Other operators will be discussed in the relevant sections.
Previous | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Next |
No comments:
Post a Comment