Hassan Tariq INTRODUCTION TO SQL What is SQL? –When a user wants to get some information from a database file, he can issue a query. – A query is a user–request.

Slides:



Advertisements
Similar presentations
Structured Query Language (SQL)
Advertisements

Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.
1 Lecture 02: SQL. 2 Outline Data in SQL Simple Queries in SQL (6.1) Queries with more than one relation (6.2) Recomeded reading: Chapter 3, Simple Queries.
SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: SQL92, SQL2, SQL3. Vendors support.
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
1 Lecture 12: SQL Friday, October 26, Outline Simple Queries in SQL (5.1) Queries with more than one relation (5.2) Subqueries (5.3) Duplicates.
Introduction to Structured Query Language (SQL)
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #10 Matthew P. Johnson Stern School of Business, NYU Spring,
Introduction to Structured Query Language (SQL)
Matthew P. Johnson, OCL1, CISDD CUNY, F20041 OCL1 Oracle 8i: SQL & PL/SQL Session #3 Matthew P. Johnson CISDD, CUNY Fall, 2004.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #10 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
1 Lecture 02: Basic SQL. 2 Outline Data in SQL Simple Queries in SQL Queries with more than one relation Reading: Chapter 3, “Simple Queries” from SQL.
Structured Query Language Part I Chapter Three CIS 218.
Introduction to Structured Query Language (SQL)
1 Lecture 2: SQL Wednesday, January 7, Agenda Leftovers from Monday The relational model (very quick) SQL Homework #1 given out later this week.
1 Information Systems Chapter 6 Database Queries.
Complex Queries (1) Product ( pname, price, category, maker)
Structured Query Language Introduction to SQL What is SQL? – –When a user wants to get some information from a database file, he can issue a query.1.
1. Midterm summary Types of data on the web: unstructured, semi- structured, structured Scale and uncertainty are key features Main goals are to model,
Introduction to SQL J.-S. Chou Assistant Professor.
IM433-Industrial Data Systems Management Lecture 5: SQL.
Intro. to SQL DSC340 Mike Pangburn. Learning Objectives Understand the data-representation terminology underlying relational databases Understand core.
More SQL: Complex Queries, Triggers, Views, and Schema Modification UMM AL QURA UNIVERSITY College of Computer Dr. Ali Al Najjar 1.
Structured Query Language ( 結構化查詢語言 ) Database Programming CREATE TABLE … ALTER TABLE… ADD/DROP/MODIFY (…) DESC … DROP TABLE … RENAME … TO … INSERT INTO.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
SQL SQL Review. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
Structured Query Language ( 結構化查詢語言 ) Lesson Review What is S Q L?L? When a user wants to get some information from a database file, he can issue a _______.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Hassan Tariq MULTIPLE TABLES: SQL provides a convenient operation to retrieve information from multiple tables.SQL provides a convenient operation to.
1 Introduction to Database Systems CSE 444 Lecture 02: SQL September 28, 2007.
1 Lecture 02: SQL Friday, September 30, Administrivia Homework 1 is out. Due: Wed., Oct. 12 Did you login on IISQLSRV ? Did you change your password.
Lectures 2&3: Introduction to SQL. Lecture 2: SQL Part I Lecture 2.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
SQL BY MR.PRASAD SAWANT INDIA Introduction to SQL What is SQL? –When a user wants to get some information from a database file, he can issue a query.1.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
CSE 326: Data Structures Lecture #22 Databases and Sorting Alon Halevy Spring Quarter 2001.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
Create Stored Procedures and Functions Database Management Fundamentals LESSON 2.4.
CHAPTER 6: INTRODUCTION TO SQL © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer,
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3),
SQL Query Getting to the data ……..
SQL.
Database practice session
Structured Query Language
Server-Side Application and Data Management IT IS 3105 (FALL 2009)
Programming with Android:
Structured Query Language
Structured Query Language
Introduction to SQL Wenhao Zhang October 5, 2018.
SQL.
Database systems Lecture 3 – SQL + CRUD
SQL Introduction Standard language for querying and manipulating data
Lecture 12: SQL Friday, October 20, 2000.
Lectures 3: Introduction to SQL 2
Introduction to Database Systems CSE 444 Lecture 02: SQL
Contents Preface I Introduction Lesson Objectives I-2
Lectures 2: Introduction to SQL 1
Instructor: SAMIA ARSHAD
Presentation transcript:

Hassan Tariq

INTRODUCTION TO SQL What is SQL? –When a user wants to get some information from a database file, he can issue a query. – A query is a user–request to retrieve data or information with a certain condition. –SQL is a query language that allows user to specify the conditions. (instead of algorithms)

INTRODUCTION TO SQL Concept of SQL –The user specifies a certain condition. –The result of the query will then be stored in form of a table. –Statistical information of the data. –The program will go through all the records in the database file and select those records that satisfy the condition.(searching).

Data Definition Language Data Definition Language Create/alter/drop Data Manipulation Language Data Manipulation Language Insert/delete/updat e Transact-SQL Transact-SQL a sequence of SQL statements

FROM 1 WHERE 2 GROUP BY 3 HAVING 4 SELECT 5 ORDER BY 6

7 Basic form: (plus many many more bells and whistles) SELECT attributes FROM relations (possibly multiple, joined) WHERE conditions (selections) SELECT attributes FROM relations (possibly multiple, joined) WHERE conditions (selections)

8 PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi SELECT * FROM Product WHERE category=‘Gadgets’ Product PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks “selection”

9 SELECT Name, Price, Manufacturer FROM Product WHERE Price > 100 Product(PName, Price, Category, Manfacturer) Answer(PName, Price, Manfacturer) Input Schema Output Schema

10 What goes in the WHERE clause:  x = y, x < y, x <= y, etc  For number, they have the usual meanings  For CHAR and VARCHAR: lexicographic ordering  Expected conversion between CHAR and VARCHAR  For dates and times, what you expect...  Pattern matching on strings: s LIKE p (next)

11  s LIKE p: pattern matching on strings  p may contain two special symbols:  % = any sequence of characters  _ = any single character Product(Name, Price, Category, Manufacturer) Find all products whose name mentions ‘gizmo’: SELECT * FROM Products WHERE PName LIKE ‘%gizmo%’

12 PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi SELECT PName, Price, Manufacturer FROM Product WHERE Category IN (‘Photography’,’Household’) Product PNamePriceManufacturer SingleTouch$149.99Canon MultiTouch$203.99Hitachi Use of IN

13 PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi SELECT PName, Price, Manufacturer FROM Product WHERE Price BETWEEN 19 and 30 Product PNamePriceManufacturer Gizmo$19.99GizmoWorks Powergizmo$29.99GizmoWorks Use of BETWEEN

14 SELECT DISTINCT category FROM Product SELECT DISTINCT category FROM Product Compare to: SELECT category FROM Product SELECT category FROM Product Category Gadgets Photography Household Category Gadgets Photography Household

COUNT() Function SELECT COUNT(column_name) FROM table_name SUM() Function SELECT SUM(column_name) FROM table_name MAX() Function SELECT MAX(column_name) FROM table_name MIN() Function SELECT MIN(column_name) FROM table_name

AVG() Function SELECT AVG(column_name) FROM table_name IS NULL Clause select * from STUDENT where name is null TOP Clause SELECT TOP 2 * FROM Persons

17 SELECT pname, price, manufacturer FROM Product WHERE category=‘gizmo’ AND price > 50 ORDER BY price, pname SELECT pname, price, manufacturer FROM Product WHERE category=‘gizmo’ AND price > 50 ORDER BY price, pname Ordering is ascending, unless you specify the DESC keyword. Ties are broken by the second attribute on the ORDER BY list, etc.

18 SELECT Category FROM Product ORDER BY PName SELECT Category FROM Product ORDER BY PName PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi ?

GROUPING SELECT FROM WHERE condition ; GROUP BY groupexpr [HAVING requirement] Group functions: COUNT( ), SUM( ), AVG( ), MAX( ), MIN( ) COUNT( ), SUM( ), AVG( ), MAX( ), MIN( ) –groupexpr specifies the related rows to be grouped as one entry. Usually it is a column. –WHERE condition specifies the condition of individual rows before the rows are group. HAVING requirement specifies the condition involving the whole group.

20 PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi SELECT Manufacturer, COUNT(*) as Total FROM Product GROUP BY Manufacturer Product ManufacturerTotal GizmoWorks2 Canon1 Hitachi1 Use of GROUP BY

21 PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi SELECT Manufacturer, COUNT(*) as Total FROM Product GROUP BY Manufacturer HAVING COUNT(*)>2 Product ManufacturerTotal GizmoWorks2 Use of GROUP BY with HAVING

UNION, INTERSECTION AND DIFFERENCE OF TABLES UNION, INTERSECTION AND DIFFERENCE OF TABLES AB The union of A and B (A  B) A table containing all the rows from A and B.

UNION, INTERSECTION AND DIFFERENCE OF TABLES UNION, INTERSECTION AND DIFFERENCE OF TABLES The intersection of A and B (A  B) A table containing only rows that appear in both A and B. AB

UNION, INTERSECTION AND DIFFERENCE OF TABLES UNION, INTERSECTION AND DIFFERENCE OF TABLES The difference of A and B (A–B) A table containing rows that appear in A but not in B. AB

25 PNameCategory GizmoGadgets PowergizmoGadgets SELECT PName FROM ProductSET1 UNION SELECT PName FROM ProductSET2 ProductSet1 PName Gizmo Powergizmo MicroUSB UNION PNameCategory MicroUSBGadgets PowergizmoGadgets ProductSet2 SELECT PName FROM ProductSET1 UNION ALL SELECT PName FROM ProductSET2 PName Gizmo Powergizmo MicroUSB Powergizmo UNION ALL

26 PNameCategory GizmoGadgets PowergizmoGadgets SELECT PName FROM ProductSET1 INTERSECT SELECT PName FROM ProductSET2 ProductSet1 PName Powergizmo INTERSECT PNameCategory MicroUSBGadgets PowergizmoGadgets ProductSet2

BASIC SQL ADVANCED SQL