Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete.

Slides:



Advertisements
Similar presentations
Introduction to SQL, OleDB interface to Access from VB.NET.
Advertisements

CSC271 Database Systems Lecture # 11.
Greg Riccardi Florida State University. Using SQL to Manipulate Database Content and Structure How to create queries in SQL –Simple select statements.
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Introduction to Structured Query Language (SQL)
SQL SQL (Structured Query Language) is used to define, query, and modify relational databases Every relational database system understands SQL SQL is standard:
SQL Basics Based on the relational algebra we just learned. Nonprocedural language – what to be done not how Simple, powerful language Used for both data.
SQL Neyha Amar CS 157A, Fall Inserting The insert statement is used to add a row of data into a table Strings should be enclosed in single quotes,
Chapter 6 SQL: Data Manipulation. 2 Objectives of SQL u Database language should allow user to: –create database and relation structures –perform insertion,
Introduction to Structured Query Language (SQL)
Microsoft Access 2010 Chapter 7 Using SQL.
Introduction to SQL J.-S. Chou Assistant Professor.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform.
Data Manipulation Using MySQL tMyn1 Data Manipulation Using MySQL Ideally, a database language should allow a user to: –Create the database and relation.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
IST 210 SQL Todd Bacastow IST 210: Organization of Data.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
1 Querying a Single Table Structured Query Language (SQL) - Part II.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
1 SQL SQL (Structured Query Language) : is a database language that is used to create, modify and update database design and data. Good Example of DBMS’s.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
# 1# 1 QueriesQueries How do we ask questions of the data? What is SELECT? What is FROM? What is WHERE? What is a calculated field? Spring 2010 CS105.
SQL and QBE Transparencies. ©Pearson Education 2009 Chapter 3 - Objectives Purpose and importance of SQL, the main language for querying relational databases.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
SQL: Additional Notes. 2 Example 5.3 Use of DISTINCT List the property numbers of all properties that have been viewed. SELECT propertyNo FROM Viewing;
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Teacher Workshop Database Design Pearson Education © 2014.
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
Subqueries Schedule: Timing Topic 25 minutes Lecture
The Database Exercises Fall, 2009.
Working with Tables: Join, Functions and Grouping
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
© Pearson Education Limited, 2004
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL LANGUAGE and Relational Data Model TUTORIAL
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Structured Query Language
Structured Query Language – The Fundamentals
M1G Introduction to Database Development
Chapter Name SQL: Data Manipulation
Subqueries Schedule: Timing Topic 25 minutes Lecture
Shelly Cashman: Microsoft Access 2016
Chapter Name SQL: Data Manipulation
Presentation transcript:

Session 4 SQL Structured Query Language

SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete

Access SQL Where to practice your SQL.

Main SQL Statements SELECT INSERT UPDATE DELETE

Rules & Conventions SQL is free-format, like HTML, but typing clauses on separate lines makes it easier to read. Type reserved words in capitals. All non-numeric data must be enclosed in single quotes e.g. 'myname'. The order of the clauses in a SELECT statement cannot be changed. Close all queries with a semi-colon ;

List all Employees SELECT EmpNo, Bno, Name, Address, DOB, Salary FROM Employee; Employee {Empno, Bno, Name, Address, DOB, Position, Salary} Corresponds to field names in the database. Tip: don't leave spaces in field names when creating the DB. * = Wildcard character. In SQL this is used to return all records. Corresponds to table names in the database. SELECT * FROM Employee;

Retrieve an Ordered List SELECT Name, Address FROM Employee ORDER BY Name; Employee {Empno, Bno, Name, Address, DOB, Position, Salary}

How Many Employees Share the Same Postcode? SELECT Postcode, COUNT(*) AS Total FROM Employee GROUP BY Postcode; Employee {Empno, Bno, Name, Address, DOB, Position, Salary} PostcodeTotal B15 1BQ6 B3 1LX3 B3 2AS2 B4 6DD1

Employee's Monthly Salary? SELECT Name, Salary/12 FROM Employee; Employee {Empno, Bno, Name, Address, DOB, Position, Salary} NameExpr1001 Smith Foster Jones Patel Chen Mathematical Operators + Add - Subtract * Multiply / Divide Note the default column name.

SQL – Aggregate Functions MIN Returns lowest value in a set of values MAX Returns highest value in a set of values SUM Provides the sum of a set of values. COUNT AVG Returns average of a set of values. Nulls are ignored.

Total Salary Bill for the Year SELECT SUM(Salary) AS TotalSalaries FROM Employee; TotalSalaries £142, Employee {Empno, Bno, Name, Address, DOB, Position, Salary}

Comparison Operators =equals <is less than >is greater than <= is less than or equal to >=is greater than or equal to <>is not equal to !=is not equal to

Where Five basic search conditions Comparison Compare the value of one expression to the value of another expression Range Test whether the value of an expression falls within a specified range of values Set Membership Test whether the value of an expression equals one of a set of values Pattern Match Test whether a string matches a specified pattern Null Test whether a column has a null value

Comparison - Salary Above £N SELECT Name, Salary FROM Employee WHERE Salary >= ORDER BY Salary; NameSalary Smith£32, Chen£31, Foster£28, Employee {Empno, Bno, Name, Address, DOB, Position, Salary}

Pattern Match – Postcode = EC1 SELECT Name, Postcode FROM Employees WHERE Postcode LIKE 'EC1*'; NamePostcode Caroline CumminsEC1A 7DD Peter LongEC1A 4AB Andrew RawstronEC16 8TY Employee {Empno, Bno, Name, Address, DOB, Position, Salary} * = MS Access % = ISO SQL

Range - Between SELECT * FROM Employee WHERE DOB BETWEEN #1/10/2004# AND #31/10/2004#; Employee {Empno, Bno, Name, Address, DOB, Position, Salary}

Set Membership - Find all Employees who are Secretaries or Managers SELECT * FROM Employee WHERE Position IN (‘Secretary’, ‘Manager’); SELECT * FROM Employee WHERE Position = ‘Secretary’ OR Position = ‘Manager’; Employee {Empno, Bno, Name, Address, DOB, Position, Salary}

Subquery – Lowest Paid Worker SELECT Name, Salary FROM Employee WHERE Salary = (SELECT MIN(Salary) FROM Employee); NameSalary Jones£25, Employee {Empno, Bno, Name, Address, DOB, Position, Salary}

Subqueries Cannot use ORDER BY in subqueries. Cannot perform on multiple attributes. Cannot use LIKE or BETWEEN.

SELECT DISTINCT Prevents duplicate tuples appearing in query results - compares whole ROWS only.

Simple join queries Example join operation in SQL –SELECT * FROM Employee, TimeCard WHERE Employee.ssn = TimeCard.ssn

Outer join queries in Access

Queries with multiple operators Example of SQL statement with 3 joins: SELECT lastName, firstName, title, dateRented FROM Movie, Video, PreviousRental, Customer WHERE Movie.movieID = Video.movieID AND Customer.accountID = PreviousRental.accountID AND Video.videoID = PreviousRental.videoID AND dateRented > #01/12/2003#

SQL exercises