week# 12 haifa abulaiha november 02, 2015 1

12
WEEK# 12 Haifa Abulaiha h [email protected] November 02, 2015 1

Upload: gordon-mitchell

Post on 18-Jan-2018

214 views

Category:

Documents


0 download

DESCRIPTION

SQL SELECT Select statement is used to retrieve data from tables in a database. Select specifies the fields to include in the query. FROM key word specifies the tables where the fields can be found. Example: SELECT Students.FirstName, Students.LastName FROM Students; SELECT * FROM Students; 3

TRANSCRIPT

Page 2: WEEK# 12 Haifa Abulaiha November 02, 2015 1

2

SQL

• Structure Query Language.• SQL is a standard language to define,

manipulate and retrieve data in database.• Basic SQL keywords:

– SELECT– FROM– WHERE– ORDER BY

Page 3: WEEK# 12 Haifa Abulaiha November 02, 2015 1

3

SQL SELECT• Select statement is used to retrieve data from

tables in a database.• Select specifies the fields to include in the query.• FROM key word specifies the tables where the

fields can be found.• Example:

SELECT Students.FirstName, Students.LastName FROM Students;

SELECT * FROM Students;

Page 4: WEEK# 12 Haifa Abulaiha November 02, 2015 1

4

SQL ORDER BY

• Determines how the rows will be sorted.• Example:

SELECT * FROM Students ORDER BY Students.LastName, Students.FirstName;

SELECT * FROM Students ORDER BY Students.LastName DESC;

Page 5: WEEK# 12 Haifa Abulaiha November 02, 2015 1

5

SQL JOIN

• To include results from multiple tables.• JOIN

– INNER– LEFT– Right

• Example:SELECT Students.FirstName, Students.LastName, StudentAddresses.Address,FROM Students INNER JOIN StudentAddresses ON Students.StudentID=StudentAddresses.StudentID;

Page 6: WEEK# 12 Haifa Abulaiha November 02, 2015 1

6

SQL GROUP BY

• To calculate statistics• Example:

SELECT Students.FirstName, Students.LastName, Count (StudentAddresses.Address) AS CountOfAddressType FROM Students INNER JOIN StudentAddresses ON Students.StudentID=StudentAddresses.StudentIDGROUP BY Students.FirstName, Students.LastName;

Page 7: WEEK# 12 Haifa Abulaiha November 02, 2015 1

7

SQL WHERE

• Sets the criteria that records must match to be included in the results.

• If the query does not include WHERE, the query will return all records.

• Example:SELECT Students.FirstName WHERE Students.ID = “123456789”;

Page 8: WEEK# 12 Haifa Abulaiha November 02, 2015 1

8

SQL INSERT

• To insert a new record in a table in the database.

• Example:INSERT INTO Students VALUES (“Sarah”, “Joseph” , “123456789”);

INSERT INTO Students (FirstName, LastName, StudentID) VALUES (“Sarah”, “Joseph” , “123456789”);

Page 9: WEEK# 12 Haifa Abulaiha November 02, 2015 1

9

SQL UPDATE

• To update records in a table in the database.

• Example:Update StudentAddresses SET StudentAddresses.AddressType = “Mailing” WHERE StudentAddresses.AddressType=“Home”;

Page 10: WEEK# 12 Haifa Abulaiha November 02, 2015 1

10

SQL DELETE

• To delete records from a table in the database.

• Example:DELETE FROM StudentAddresses WHERE StudentAddresses.AddressType=“Parent”;

Page 11: WEEK# 12 Haifa Abulaiha November 02, 2015 1

11

IMPORTANT DATESDue

MyITLab Lesson D Monday 11/02

Homework # 5 Friday 11/06

Exam 2 Section 19 Monday 11/09

Exam 2 Section 21 Wednesday 11/11

MyITLab Bonus Project # 2 Friday 11/13

MyITLab Lesson E Monday 11/16

Page 12: WEEK# 12 Haifa Abulaiha November 02, 2015 1