SQL Basic Queries
SQL Basic Queries
SELECT Statement
Use the SELECT statement to retrieve data from one or more tables:
SELECT <column(s) > FROM <table> [WHERE <condition>] [ORDER BY <column(s) [ASC|DESC]>]
table column is the name of the table is the name of the column in the table to be selected
condition
identifies the rows to be selected and is composed of column names, expressions, constraints,sub-queries and comparison operators
is the name of the column(s) used for sorting
SELECT * FROM emp Projection : Querying for projection retrieves a subset of the columns in one or more tables. It is best to put each column chosen in a separate line
Example
SELECT
FROM emp
Arithmetic Operators
Operator + / * % Operation Addition Subtraction Division Multiplication Modulo
Description
Total of the (distinct) values in the expression Average of the (distinct) values in the expression Number of (distinct) non-null values in the expression Number of selected rows including null values Highest value in the expression Lowest value in the expression
SELECT EmployeeId,
LastName
IN CONDITION
Display rows based on a list of values SELECT EmployeeId FROM Employee WHERE ManagerId IN (100, 200, 300)
LastName,
JobId Employee
SELECT
FROM WHERE
OR
LastName,
JobId Employee Salary <= 15000 JobId = SE
SELECT FROM
Top
selEct FROM
Sorting Rows
ORDER BY clause
ASC specifies an ascending order DESC specifies a descending order
SELECT
LastName, Salary, JobId
Display the result in descending order by the attribute salary If two records have the same attribute value, the salary sorting criteria is in ascending order according to the attribute values of JobId
Copyright EME TECHNOLOGIES
SELECT
Resources
You may find useful content on T-SQL joins on the following url:
Web sites: http://www.sqlteam.com/article/writing-outer-joins-in-t-sql http://www.sqlmag.com/Article/ArticleID/5342/sql_server_5342.html http://pietschsoft.com/post/2005/12/T-SQL-Join-Tables-by-a-Fieldthat-contains-a-delimited-string.aspx ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/udb9/html/c4f98374e8a7-4f73-8d0e-309bd94ad1ae.htm
Key Points
SQL is an industry standard language for updating to, and getting information from, a database. The basic and most common SQL statements are: SELECT, INSERT, UPDATE, DELETE.