0% found this document useful (0 votes)
16 views20 pages

Lab 4

The document provides an overview of SQL operators such as AND, OR, and JOIN, with examples demonstrating their usage in querying databases. It explains how to select records from a table based on specific conditions and how to link multiple tables to retrieve related data. Different types of SQL JOINs, including INNER, LEFT, RIGHT, and FULL OUTER JOIN, are also described with examples to illustrate their functionality.

Uploaded by

zainab.haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views20 pages

Lab 4

The document provides an overview of SQL operators such as AND, OR, and JOIN, with examples demonstrating their usage in querying databases. It explains how to select records from a table based on specific conditions and how to link multiple tables to retrieve related data. Different types of SQL JOINs, including INNER, LEFT, RIGHT, and FULL OUTER JOIN, are also described with examples to illustrate their functionality.

Uploaded by

zainab.haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

lab4

By: zainab mohammed && zainab


haider
‫‪SQL AND Operator‬‬

‫‪, ...‬العمود االول‪ ,‬العمود الثاني ‪SELECT‬‬


‫اسم الجدول ‪FROM‬‬
‫;‪ ...‬شرط ثالث ‪ AND‬شرط ثاني ‪ AND‬شرط اول ‪WHERE‬‬
Example:
Select all customers from Spain that starts with the letter 'G':

CustomerI CustomerN ContactNa Address City PostalCode Country


D ame me
1 Alfreds Maria Obere Str. Berlin 12209 Germany
Futterkiste Anders 57

2 Ana Trujillo Ana Trujillo Avda. de la México D.F. 05021 Mexico


Emparedad Constitució
os y n 2222
helados

3 Antonio Antonio Mataderos México D.F. 05023 Mexico


Moreno Moreno 2312
Taquería

4 Around the Thomas 120 London WA1 1DP UK


Horn Hardy Hanover
Sq.

5 Berglunds Christina Berguvsväg Luleå S-958 22 Sweden


snabbköp Berglund en 8
• SELECT * FROM Customers
WHERE Country = 'Spain' AND CustomerName LIKE 'G%';

CustomerID CustomerNam ContactName Address City PostalCode Country


e
29 Galería del Eduardo Rambla de Barcelona 08022 Spain
gastrónomo Saavedra Cataluña, 23
30 Godos Cocina José Pedro C/ Romero, Sevilla 41101 Spain
Típica Freyre 33
‫‪SQL OR Operator‬‬

‫‪, ...‬العمود االول‪ ,‬العمود الثاني ‪SELECT‬‬


‫اسم الجدول ‪FROM‬‬
‫;‪ ...‬شرط ثالث ‪ OR‬شرط ثاني ‪ OR‬شرط اول ‪WHERE‬‬
Example: Get your own SQL Server Select all customers
from Germany or Spain CustomerI CustomerN ContactNa Address
D ame me
City PostalCode Country

1 Alfreds Maria Obere Str. Berlin 12209 Germany


Futterkiste Anders 57
6 Blauer See Hanna Forsterstr. Mannheim 68306 Germany
Delikatess Moos 57
en

• SELECT * FROM Customers 8 Bólido


Comidas
Martín
Sommer
C/ Araquil,
67
Madrid 28023 Spain

preparadas

WHERE Country = 'Germany' OR Country = 'Spain'; 17 Drachenbl


ut
Sven
Ottlieb
Walserweg Aachen
21
52066 Germany

Delikatess
end
22 FISSA Diego Roel C/ Madrid 28034 Spain
Fabrica Moralzarza
Inter. l, 86
Salchichas
S.A.
25 Frankenver Peter Berliner München 80805 Germany
sand Franken Platz 43
29 Galería del Eduardo Rambla de Barcelona 08022 Spain
gastrónom Saavedra Cataluña,
o 23
30 Godos José Pedro C/ Romero, Sevilla 41101 Spain
Cocina Freyre 33
Típica
39 Königlich Philip Maubelstr. Brandenbu 14776 Germany
Essen Cramer 90 rg
44 Lehmanns Renate Magazinw Frankfurt 60528 Germany
Marktstan Messner eg 7 a.M.
d
52 Morgenste Alexander Heerstr. Leipzig 04179 Germany
rn Feuer 22
Gesundkos
t
56 Ottilies Henriette Mehrheim Köln 50739 Germany
Käseladen Pfalzheim erstr. 369
63 QUICK- Horst Taucherstr Cunewalde 01307 Germany
Stop Kloss aße 10
69 Romero y Alejandra Gran Vía, Madrid 28001 Spain
tomillo Camino 1
79 Toms Karin Luisenstr. Münster 44087 Germany
Spezialität Josephs 48
en
86 Die Rita Adenauera Stuttgart 70563 Germany
Wandernd Müller llee 900
e Kuh
SQL JOIN

• It is a process of linking between database


tables, and this is done by linking between
rows that represent multiple data for each
table. JOINS is used to query data from more
than one table simultaneously
Example

• "Orders" table:
OrderID CustomerID OrderDate
10308 2 1996-09-18
10309 37 1996-09-19
10310 77 1996-09-20

• "Customers" table
CustomerID CustomerName ContactName Country
1 Alfreds Futterkiste Maria Anders Germany
2 Ana Trujillo Emparedados Ana Trujillo Mexico
y helados
3 Antonio Moreno Taquería Antonio Moreno Mexico
• SELECT Orders.OrderID, Customers. CustomerName,
Orders. OrderDate
• FROM Orders
• INNER JOIN Customers
• ON Orders. CustomerID=Customers.CustomerID
Different Types of SQL JOINs

• (INNER) JOIN: Returns records that have matching


values in both tables
• LEFT (OUTER) JOIN: Returns all records from the left
table, and the matched records from the right table
• RIGHT (OUTER) JOIN: Returns all records from the
right table, and the matched records from the left
table
• FULL (OUTER) JOIN: Returns all records when there
is a match in either left or right table
LEFT JOIN

"Customers" table "Orders" table

CustomerID CustomerN ContactNa Address City PostalCode Country


ame me

1 Alfreds Maria Obere Str. Berlin 12209 Germany


Futterkiste Anders 57
OrderID CustomerID EmployeeID OrderDate ShipperID
10308 2 7 1996-09-18 3
2 Ana Trujillo Ana Trujillo Avda. de la México D.F. 05021 Mexico
Emparedad Constitució 10309 37 3 1996-09-19 1
os y n 2222
helados 10310 77 8 1996-09-20 2

3 Antonio Antonio Mataderos México D.F. 05023 Mexico


Moreno Moreno 2312
Taquería
Example:select all customers, and any orders they
might have:

‫وتعني هنا اداة شرط‬


ON
CustomerName OrderID
Alfreds Futterkiste
Ana Trujillo Emparedados y helados 10308
Antonio Moreno Taquería 10365
Around the Horn 10383
Around the Horn 10355
Berglunds snabbköp 10278
Berglunds snabbköp 10280
Berglunds snabbköp 10384
Blauer See Delikatessen
RIGHT JOIN
Orders" table Employees" table
OrderID CustomerID EmployeeID OrderDate ShipperID EmployeeID LastName FirstName BirthDate Photo

10308 2 7 1996-09-18 3 1 Davolio Nancy 12/8/1968 EmpID1.pic

10309 37 3 1996-09-19 1 2 Fuller Andrew 2/19/1952 EmpID2.pic

10310 77 8 1996-09-20 2
3 Leverling Janet 8/30/1963 EmpID3.pic
Example: return all employees, and any orders they
might have placed

OrderID LastName FirstName


West Adam
10248 Buchanan Steven
10249 Suyama Michael
10250 Peacock Margaret
10251 Leverling Janet
10252 Peacock Margaret
10253 Leverling Janet
10254 Buchanan Steven
‫‪FULL OUTER JOIN‬‬
‫)‪(s‬اسم الحقل ‪• SELECT‬‬
‫الجدول االول ‪FROM‬‬
‫الجدول الثاني ‪FULL OUTER JOIN‬‬
‫الجدول االول‪.‬اسم عمود = الجدول الثاني‪.‬اسم عمود ‪ON‬‬
‫;الشرط ‪WHERE‬‬
"Customers" table: "Orders" table:
CustomerID CustomerN ContactNa Address City PostalCode Country OrderID CustomerID EmployeeID OrderDate ShipperID
ame me

1 Alfreds Maria Obere Str. Berlin 12209 Germany


Futterkiste Anders 57 10308 2 7 1996-09-18 3

2 Ana Trujillo Ana Trujillo Avda. de la México D.F. 05021 Mexico


Emparedad Constitució 10309 37 3 1996-09-19 1
os y helados n 2222

10310 77 8 1996-09-20 2

3 Antonio Antonio Mataderos México D.F. 05023 Mexico


Moreno Moreno 2312
Taquería
• SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Ord
ers.CustomerID
ORDER BY Customers.CustomerName

CustomerName OrderID
Null 10309
Null 10310
Alfreds Futterkiste Null
Ana Trujillo Emparedados y helados 10308
Antonio Moreno Taquería

You might also like