Vous êtes sur la page 1sur 22

DATABASE SYSTEMS II

Chapter 4

T-SQL Query
Amoud University
Faculty of Computing and ICT
Senior BBIT

Lecturer : Samakaab Basha


T-SQL
 T-SQL (Transact-SQL) is a set of programming
extensions from Sybase and Microsoft that add several
features to the Structured Query Language (SQL).
Transact-SQL is Microsoft's and Sybase's proprietary
extension to the SQL used to interact with relational
databases.
Where Clause
The WHERE clause sets a conditional statement, and it
can be used with any type of SQL query. As the select
query executes, SQL processes one row at a time.

Each time the conditional statement is met (returns


true), a row is returned as a result. SQL WHERE is
essentially, a filtering mechanism for SQL queries and is
a tremendous asset to any aspiring SQL developer.
Examples
SELECT *
FROM TestTable
WHERE customer = ‘Ali'
DELETE
from TestTable
WHERE ID = ‘10’
UPDATE TestTable
SET Age= ‘15’
WHERE Name = ‘Ali';
ID Customer Day_of_order Product Quantity

1 ALI 2008-08-01 Pen 40

2 MUUSE 2008-08-01 Stapler 10

3 MUNA 2008-07-25 19" LCD Screen 30

4 NOUR 2008-07-25 HP Printer 20


2008-07-25
5 KHALID IPhone 12
Where With operators
Conditional statements are not unique to SQL, and
neither are operators. Operators are symbols such as
(=), (<) or (>), and they are seen inside of
conditional statements and expressions in SQL and
other programming languages.
Where With Multiple Conditionals
A WHERE statement can accept multiple conditional
statements. What this means is that we are able to select rows
meeting two different conditions at the same time.

SQL Where And:


USE mydatabase;

SELECT *
FROM orders
WHERE day_of_order > '7/31/08'
AND customer = 'Tizag'
AS
•SQL AS This Query temporarily assigns a table
column a new name. This grants the SQL
developer the ability to make adjustments to the
presentation of query results and allow the
developer to label results more accurately without
permanently renaming table columns.
Example

Select DayOfOrder AS "Date",


customer As "Client",
Product AS “Tools”,
Quantity AS “Number”
From TestTable;
Operators
• SQL operators are found in just about every SQL query.
Operators are the mathematical and equality symbols used
to compare, evaluate, or calculate values. Equality
operators include the (<), (>), and (=) symbols, which
are used to compare one value against another.
Between
 Between is a conditional statement found in
the WHERE clause. It is used to query for table
rows that meet a condition falling between a
specified range of numeric values. It would be used
to answer questions like, "How many orders did we
receive BETWEEN July 20th and August 5th?“

Between essentially combines two conditional


statements into one
Example
Select *
From TestTable
Where Date Between '7/20/08' AND '8/05/08';

Select *
From TestTable
Where Date >= '7/20/08'
AND Date <= '8/05/08';
Order By
 Order By : is the SQL command used to sort
rows as they are returned from a SELECT query.
SQL order by command may be added to the end
of any select query and it requires at least one
table column to be specified in order for SQL to
sort the results.
Example
SELECT *
FROM TestTable
ORDER BY Date;
Ascending & Descending
The default sort order for ORDER BY is an
ascending list, [a - z] for characters or [0 - 9] for
numbers. As an alternative to the default sorting
for our results, which is ASCENDING (ASC), we
can instead tell SQL to order the table columns in
a DESCENDING (DESC) fashion [z-a].
Example
SELECT *
FROM TestTable
WHERE customer = ‘Ali'
ORDER BY Date DESC
Sorting On Multiple Columns
Results may be sorted on more than one column
by listing multiple column names in the Order
By clause, similar to how we would list column
names in each Select statement.

Example:

SELECT *
FROM TestTable
ORDER BY customer, Date;
Distinct
SQL SELECT DISTINCT is a very useful way
to eliminate retrieving duplicate data reserved for
very specific situations. To understand when to
use the DISTINCT command, let's look at a real
world example where this tool will certainly
come in handy.
Example

SELECT DISTINCT customer


FROM TestTable

Select Distinct Name


From TestTable
Subqueries
•Subqueries are query statements inserted inside of
query statements. Like the order of operations from
your high school Algebra class, order of operations
also come into play when you start to embed SQL
commands inside of other SQL commands
(subqueries).
•A Subquery or Inner query or a Nested query is
a query within another SQL query and embedded
within the WHERE clause.
Example
Select Max(date)
From TestTable SELECT *
FROM orders
WHERE Date = (SELECT
MAX(day_of_order)
Select * FROM TestTable)
From Customers Where Id In
(Select Id
From Customers
Where Salary > 4500) ;
END

Vous aimerez peut-être aussi