You are on page 1of 19

Querying and Reporting

Copyright EME TECHNOLOGIES

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

column (order by)

Copyright EME TECHNOLOGIES

Querying and Reporting


Selection : Querying for selection retrieves a subset of the rows in one or more tables.
Example

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

empno, ename, deptno

FROM emp

Copyright EME TECHNOLOGIES

Arithmetic Operators
Operator + / * % Operation Addition Subtraction Division Multiplication Modulo

Copyright EME TECHNOLOGIES

Use of Aggregate Functions


Aggregate functions are used to summarize the data retrieved in a query.
Aggregate Function
Sum ([all | distinct] expression) Avg ([all | distinct] expression) Count ([all | distinct] expression) count(*) Max (expression) Min (expression)

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

Copyright EME TECHNOLOGIES

Selecting Rows Search Based


The method of restriction is the basis of the WHERE clause in SQL Character strings and dates in the WHERE clause must be enclosed in single quotation marks ()

Copyright EME TECHNOLOGIES

Rows may be limited by:


EQUALS CONDITION
Display rows based on an exact match of values
SELECT LastName,
Salary

FROM Employee WHERE Salary = 30000

SELECT EmployeeId,
LastName

FROM Employee WHERE ManagerName = RAYMOND

Copyright EME TECHNOLOGIES

Rows may be limited by: (Continued)


>, <, <=, >= or <> CONDITION
SELECT LastName FROM Employee WHERE Salary > 30000 SELECT EmployeeId, FROM WHERE Salary <= 30000 SELECT EmployeeId FROM Employee WHERE Status <> ACTIVE
LastName Employee

Copyright EME TECHNOLOGIES

Rows may be limited by: (Continued)


BETWEEN CONDITION
Display rows based on a range of values SELECT LastName FROM Employee WHERE Salary BETWEEN 30000 AND 50000

IN CONDITION
Display rows based on a list of values SELECT EmployeeId FROM Employee WHERE ManagerId IN (100, 200, 300)

Copyright EME TECHNOLOGIES

Rows may be limited by: (Continued)


LIKE CONDITION
Performs wildcard searches of valid search string values Can contain either literal characters or numbers
% denotes zero or many characters _ denotes one character

Use ESCAPE identifier to search for the actual % and _symbols


SELECT LastName FROM Employee WHERE LastName LIKE %ram SELECT LastName FROM Employee WHERE LastName LIKE _at

Copyright EME TECHNOLOGIES

Rows may be limited by: (Continued)


LOGICAL CONDITION
AND, OR, NOT
SELECT FROM WHERE
AND

LastName,
JobId Employee

Salary <=15000 JobId = SE

SELECT
FROM WHERE
OR

LastName,
JobId Employee Salary <= 15000 JobId = SE

Copyright EME TECHNOLOGIES

Rows may be limited by: (Continued)


LOGICAL CONDITION
AND, OR, NOT SELECT FROM WHERE LastName,
JobId Employee Salary <=15000 AND NOT JobId = SE

Copyright EME TECHNOLOGIES

Rows may be limited by: (Continued)


Distinct

SELECT FROM
Top

distinct sAddress tb_Employee Top(4) iEmployeeid tb_Employee

selEct FROM

Copyright EME TECHNOLOGIES

Sorting Rows
ORDER BY clause
ASC specifies an ascending order DESC specifies a descending order
SELECT
LastName, Salary, JobId

FROM Employee ORDER BY Salary DESC, JobId ASC

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

Grouping Result Sets


Group By A group by expression can contain column names not present in the select list.

SELECT

type, avg(price), sum(ytd_sales) FROM titles Group by pub_id, type

Copyright EME TECHNOLOGIES

Grouping Result Sets


Compute clause provides a detail and summary rows with one select statement. You can calculate more than one row aggregate.
SELECT type, price, advance FROM titles Order by type Compute sum(price), sum(advance) SELECT type, price, advance FROM titles Order by type Compute sum(price), sum(advance) By type

Copyright EME TECHNOLOGIES

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

Copyright EME TECHNOLOGIES

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.

Copyright EME TECHNOLOGIES

Questions & Comments

Copyright EME TECHNOLOGIES

You might also like