Dit 216 - 2
Dit 216 - 2
Dit 216 - 2
K. O. Peasah
[email protected] // 024-426-3434
Jan 2014
2
3
4
5
6
QUERY 1: Retrieve the birth date and address of the employee(s) whose
name is ‘John B. Smith’.
9
QUERY 3: For every project located in 'Stafford', list the project
number, the controlling department number, and the department
manager's last name, address, and birthdate.
USE Northwind
SELECT CustomerID, CompanyName, ContactName
FROM Customers
WHERE (CustomerID = ‘alfki’ OR CustomerID
=‘anatr’)
ORDER BY ContactName
Ambiguous Attribute Names, Aliasing, and Tuple
Variables
• In SQL the same name can be used for two (or more) attributes as long as
the attributes are in different relations.
• If this is the case, and a query refers to two or more attributes with the same
name, we must qualify the attribute name with the relation name to prevent
ambiguity.
• This is done by prefixing the relation name to the attribute name and
separating the two by a period.
Example: Ambiguous Attribute Names
SELECT Fname, EMPLOYEE.Name, Address
FROM EMPLOYEE, DEPARTMENT
WHEREDEPARTMENT.NAME='Research' AND
DEPARTMENT.Dnumber= EMPLOYEE. .Dnumber ;
Ambiguous Attribute Names, Aliasing, and Tuple Variables
• Ambiguity also arises in the case of queries that refer to the same
relation twice, as in the following example.
• If more than one relation is specified in the FROM clause and there is
no WHERE clause, then CROSS PRODUCT – all possible tuple
combinations – of these relations is selected.
• For example, Query 6 selects all EMPLOYEE Ssns and Query 7
selects all combinations of an EMPLOYEE Ssn and a
DEPARTMENT Dname.
SELECT *
FROM EMPLOYEE
WHEREDn = 5
Use of Asterisk
SELECT *
FROM EMPLOYEE, DEPARTMENT
WHEREDname = ‘Research’ AND Dn = Dnumber
SELECT *
FROM EMPLOYEE, DEPARTMENT
Aggregate Functions in SQL
• Grouping and aggregation are required in many database
applications, SQL has features that incorporate these concepts.
• The functions SUM, MAX, MIN, and AVG are applied to a set of
multiset of numeric values and return, respectively, the sum,
maximum value, minimum value, and average (mean) of those
values.
SELECTDno,COUNT(*), AVG(Salary)
FROM EMPLOYEE
GROUP BY Dno
GROUP BY EXAMPLE
SELECT productid, count(*), sum(quantity)
FROM [order details]
GROUP BY productid
USE Northwind
INSERT INTO Territories
VALUES (98101, ‘Seatle’, 2)
INSERT –Type 2
UPDATE PROJECT
SET PLOCATION = 'Bellaire', DNUM = 5
WHERE PNUMBER=10
DELETE STATEMENT
The DELETE statement removes rows from a table
DELETE
FROM [table]
WHERE {condition}
DELETE STATEMENT - Example
This example deletes all rows from the authors table.
USE pubs
DELETE authors
DELETE STATEMENT - Example
• This example deletes all rows in which au_lname is McBadden.
USE pubs
DELETE FROM authors
WHERE au_lname = 'McBadden'
End
49
Thank You
• By using joins, you can retrieve data from two or more tables
based on logical relationships between the tables. Joins can
be specified in either the FROM or WHERE clauses.
USE Pubs
SELECT a.Au_fname, a.Au_lname, p.Pub_name
FROM Authors a LEFT OUTER JOINPublishers p
ON a.City = p.City
ORDER BY p.Pub_name ASC, a.Au_lname ASC,
a.Au_fname ASC
Using Left Outer Joins
• The only rows that are retrieved from the table to the
left are those that meet the join condition.
USE Pubs
SELECT a.Au_fname, a.Au_lname, p.Pub_name
FROM Authors a RIGHT OUTER JOIN Publishers
p
ON a.City = p.City
ORDER BY p.Pub_name ASC, a.Au_lname ASC,
a.Au_fname ASC
Using ▪Full Outer
A result Joins by a SELECT statement that
set generated
includes a full outer join includes all rows from both
tables, regardless of whether the tables have a
matching value (as defined in the join condition).
USE Pubs
SELECT a.Au_fname, a.Au_lname, p.Pub_name
FROM Authors a FULL OUTER JOIN Publishers p
ON a.City = p.City
ORDER BY p.Pub_name ASC, a.Au_lname ASC,
a.Au_fname ASC
Using Full Outer Joins
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers FULL OUTER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
Defining Subqueries inside SELECT Statement
▪ A subquery is a SELECT statement that returns a
single value and is nested inside a SELECT, INSERT,
UPDATE or DELETE statement or inside another
subquery.
Types of Subqueries
Subqueries can be specified in many places within a
SELECT statement. Statements that include a subquery
usually take one of the following formats:
• After the subquery returns the result, the outer query makes
use of it.
• There is another wildcard character you can use with LIKE operator. It
is the underscore character, ' _ ' . In a search string, the underscore
signifies a single character.
Example
• To display all the names with 'a' second character
SELECT first_name, last_name
FROM student_details
WHERE first_name LIKE '_a%';
SQL BETWEEN ... AND Operator
• The operator BETWEEN and AND, are used to compare
data for a range of values.
• For Example: to find the names of the students between age
10 to 15 years, the query would be like,
SELECT first_name, last_name, age
FROM student_details
WHERE age BETWEEN 10 AND 15;
SQL IN Operator:
• The IN operator is used when you want to compare a
column with more than one value. It is similar to an OR
condition.
• For example: If you want to find the names of students
who are studying either Maths or Science, the query
would be like,