| Start | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Using the wildcard character to select all columns in a query. Consider a database with the following two tables. Employees table:
Departments table:
Simple select statement
When used as a substitute for explicit column names, it returns all columns in all tables that a query is selecting Consider the following query: It will return all fields of all rows of the
Dot notationTo select all values from a specific table, the wildcard character can be applied to the table with dot notation. Consider the following query: This will return a data set with all fields on the |Id|FName|LName|DeptId|Name |---|---|---|--- |1|James|Smith|3|Finance |2|John|Johnson|4|IT Warnings Against Use It is generally advised that using
When Can You Use |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SELECT Using Column Aliases Column aliases are used mainly to shorten code and make column names more readable. Code becomes shorter as long table names and unnecessary identification of columns (e.g., there may be 2 IDs in the table, but only one is used in the statement) can be avoided. Along with table aliases (opens new window) this allows you to use longer descriptive names in your database structure while keeping queries upon that structure concise. Furthermore they are sometimes required, for instance in views, in order to name computed outputs. All versions of SQLAliases can be created in all versions of SQL using double quotes ( Different Versions of SQLYou can use single quotes ( Both will result in:
This statement will return
However, the explicit version (i.e., using the If the alias has a single word that is not a reserved word, we can write it without single quotes, double quotes or brackets:
A further variation available in MS SQL Server amongst others is which is equivalent to: Both will result in:
Some find using All Versions of SQLAlso, if you need to use reserved words, you can use brackets or quotes to escape: Different Versions of SQLLikewise, you can escape keywords in MSSQL with all different approaches:
Also, a column alias may be used any of the final clauses of the same query, such as an However, you may not use To create an alias from these reserved words ( This will cause numerous errors on execution. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Select Individual Columns This statement will return the columns The result will be:
If multiple tables are joined together, you can select columns from specific tables by specifying the table name before the column name: * To avoid using long table names, you can use table aliases. This mitigates the pain of writing long table names for each field that you select in the joins. If you are performing a self join (a join between two instances of the same table), then you must use table aliases to distinguish your tables. We can write a table alias like |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Selecting specified number of records The SQL 2008 standard (opens new window) defines the This standard is only supported in recent versions of some RDMSs. Vendor-specific non-standard syntax is provided in other systems. Progress OpenEdge 11.x also supports the Additionally, The following query is supported in SQL Server (opens new window) and MS Access: To do the same in MySQL (opens new window) or PostgreSQL the In Oracle the same can be done with Results: 10 records. Vendor Nuances: It is important to note that the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Selecting with Condition The basic syntax of SELECT with WHERE clause is: The [condition] can be any SQL expression, specified using comparison or logical operators like >, <, =, <>, >=, <=, LIKE, NOT, IN, BETWEEN etc. The following statement returns all columns from the table 'Cars' where the status column is 'READY': See WHERE and HAVING (opens new window) for more examples. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Selecting with CASE When results need to have some logic applied 'on the fly' one can use CASE statement to implement it. also can be chained one also can have |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Select columns which are named after reserved keywords When a column name matches a reserved keyword, standard SQL requires that you enclose it in double quotation marks: Note that it makes the column name case-sensitive. Some DBMSes have proprietary ways of quoting names. For example, SQL Server uses square brackets for this purpose: while MySQL (and MariaDB) by default use backticks: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Selecting with table alias The Employees table is given the alias 'e' directly after the table name. This helps remove ambiguity in scenarios where multiple tables have the same field name and you need to be specific as to which table you want to return data from. Note that once you define an alias, you can't use the canonical table name anymore. i.e., would throw an error. It is worth noting table aliases -- more formally 'range variables' -- were introduced into the SQL language to solve the problem of duplicate columns caused by Note that although an alias/range variable must be declared for the dervied table (otherwise SQL will throw an error), it never makes sense to actually use it in the query. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Selection with sorted Results This statement will return all the columns from the table
Or This statement changes the sorting direction. One may also specify multiple sorting columns. For example: This example will sort the results first by In order to save retyping the column name in the You may also embed a This will sort your results to have all records with the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Selecting with Aggregate functions AverageIf employee is categorized with multiple department and we want to find avg salary for every department then we can use following query. MinimumMaximumCountSum |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Selecting without Locking the table Sometimes when tables are used mostly (or only) for reads, indexing does not help anymore and every little bit counts, one might use selects without LOCK to improve performance. SQL Server MySQL Oracle DB2 where If used on table that has record modifications going on might have unpredictable results. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Select with condition of multiple values from column This is semantically equivalent to i.e. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Get aggregated result for row groups Counting rows based on a specific column value: Getting average income by department: The important thing is to select only columns specified in the There If you need to filter the results after the grouping has been done, e.g, to see only departments whose average income is larger than 1000, you need to use the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Selecting with more than 1 condition. The
This will return:
using This will return:
These keywords can be combined to allow for more complex criteria combinations: This will return:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Select rows from multiple tables This is called cross product in SQL it is same as cross product in sets These statements return the selected columns from multiple tables in one query. There is no specific relationship between the columns returned from each table. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Selecting with null Selection with nulls take a different syntax. Don't use |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Select distinct (unique values only) This query will return all
SQLFiddle Demo (opens new window) SyntaxSELECT [DISTINCT] [column1] [, [column2] ... ] RemarksSELECT determines which columns' data to return and in which order FROM a given table (given that they match the other requirements in your query specifically - where and having filters and joins). will only return results from the indicates that all columns will be returned. However, please note that it is poor practice to |
Read Article
https://develop.horje.com/learn/1434/reference