Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Advertisements

Virtual training week 4 structured query language (SQL)
Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete.
Introduction to Structured Query Language (SQL)
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.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Microsoft Access 2010 Chapter 7 Using SQL.
Chapter 7: SQL, the Structured Query Language Soid Quintero & Ervi Bongso CS157B.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
IE 423 – Design of Decision Support Systems Database development – Relationships and Queries Introduction to SQL.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
Oracle Database Administration Lecture 2 SQL language.
SQL 1: GETTING INFORMATION OUT OF A DATABASE MIS2502 Data Analytics.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Using Special Operators (LIKE and IN)
Database Programming Sections 11 & 12 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
Getting to Know SQL. © Jim Hope 2002 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement TRANSFORM.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Course title: Database-ii Chap No: 03 “Advanced SQL” Course instructor: ILTAF MEHDI.
WEEK# 12 Haifa Abulaiha November 02,
Chapter 11 Database and SQL. Flat Files and Databases Flat files Databases Advantages Efficient use of resources Access control Disadvantages Security.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
# 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.
Drill Consider the following tables with the following fields: Student: FName, LName, StudentID, Age, Yr, Course Grades: ID, P1, P2, P3 1.Display the.
MIS2502: Data Analytics SQL – Getting Information Out of a Database.
April 2002 Information Systems Design John Ogden & John Wordsworth 1 Database Design SQL (1) John Wordsworth Department of Computer Science The University.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Day 5 - More Complexity With Queries Explanation of JOIN & Examples Explanation of JOIN & Examples Explanation & Examples of Aggregation Explanation &
There’s a particular style to it… Rob Hatton
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
Tarik Booker CS 122. What we will cover… Tables (review) SELECT statement DISTINCT, Calculated Columns FROM Single tables (for now…) WHERE Date clauses,
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
How to: SQL By: Sam Loch.
Web Systems & Technologies
From: SQL From:
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL Query Getting to the data ……..
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
The Database Exercises Fall, 2009.
Structured Query Language (SQL) William Klingelsmith
Introduction To Structured Query Language (SQL)
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Database systems Lecture 3 – SQL + CRUD
Access: SQL Participation Project
Structured Query Language – The Fundamentals
M1G Introduction to Database Development
Introduction To Structured Query Language (SQL)
Contents Preface I Introduction Lesson Objectives I-2
Structured Query Language
CSC 453 Database Systems Lecture
Section 4 - Sorting/Functions
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

Getting to Know SQL

© Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION operation

© Jim Hope 2004 All Rights Reserved Data Definition CREATE TABLE statement CREATE INDEX statement ALTER TABLE statement CONSTRAINT clause DROP statement SELECT... INTO statement

© Jim Hope 2004 All Rights Reserved 1 SELECT SELECT * FROM People ; Means: Select all the fields (*) for all rows from the table called People

© Jim Hope 2004 All Rights Reserved 2 Specify Fields SELECT LastName, FirstName FROM People ; Means: Select the fields (LastName and FirstName) for all rows from the table called People

© Jim Hope 2004 All Rights Reserved 3a Setting the Scope SELECT LastName, FirstName, Score FROM People WHERE Score >=250 Means: Select the fields (LastName, FirstName, Score) for only rows where the Score is greater than or equal to 250

© Jim Hope 2004 All Rights Reserved 3b Setting the Scope SELECT LastName, FirstName, Score FROM People WHERE Score >=250 OR Score <=100 Means: Select the fields (LastName and FirstName, Score) for only rows where the Score is greater than or equal to 250 or the Score is less than or equal to 100

© Jim Hope 2004 All Rights Reserved 4a Setting the Order SELECT LastName, FirstName FROM People ORDER BY LastName ; Means: Select the fields (LastName and FirstName) for all rows from the table called People, in alphabetical (ascending) order by the values in the LastName field.

© Jim Hope 2004 All Rights Reserved 4b Setting the Order SELECT LastName, FirstName FROM People ORDER BY LastName, FirstName ; Means: Select the fields (LastName and FirstName) for all rows from the table called People, in alphabetical (ascending) order by the values in the LastName field. If there are duplicates – use the FirstName (ascending)

© Jim Hope 2004 All Rights Reserved 4c Setting the Order SELECT LastName, FirstName, Score FROM People ORDER BY Score DESC, LastName, FirstName; Means: Select the fields (LastName and FirstName) for all rows from the table called People, in (descending) order by the values in the Score field.

© Jim Hope 2004 All Rights Reserved 4d Setting the Order – you try SELECT LastName, FirstName, Score FROM People ORDER BY Score DESC What would you do if you wanted to see rows with duplicate Scores presented alphabetically by player

© Jim Hope 2004 All Rights Reserved 5a Putting things together SELECT LastName, FirstName, Score FROM People WHERE Score >=290 or Score <=100 ORDER BY Score DESC What is this doing?

© Jim Hope 2004 All Rights Reserved 5b Putting more things together SELECT LastName, FirstName, Score, City FROM People WHERE (Score >=290 or Score "Surrey" ORDER BY Score DESC What is this doing

© Jim Hope 2004 All Rights Reserved 5c Putting more things together SELECT LastName, FirstName, Score, City FROM People WHERE (Score >=290 or Score "Surrey" and City <> "Burnaby" ORDER BY Score DESC What is this doing, and what else would you add?

© Jim Hope 2004 All Rights Reserved 5d Putting more things together SELECT LastName, FirstName, Score, City FROM People WHERE (Score >=290 and City <> "Surrey") or (Score "New York") ORDER BY Score DESC What is this doing

© Jim Hope 2004 All Rights Reserved 5e Scope with IN SELECT LastName, FirstName, Score FROM People WHERE LastName IN ("Bundy", "Simpson", "Petrie"); (much better than… WHERE LastName = “Bundy” OR LastName = “Simpson” OR LastName = “Petrie”

© Jim Hope 2004 All Rights Reserved 5f Whatnot SELECT LastName, FirstName, Score FROM People WHERE LastName NOT IN ("Bundy", "Simpson", "Petrie"); Try this one

© Jim Hope 2004 All Rights Reserved 6a Counting SELECT count(*) FROM People

© Jim Hope 2004 All Rights Reserved 6b Counting SELECT count(*) FROM People WHERE Score <100

© Jim Hope 2004 All Rights Reserved 6b Counting SELECT count(*) FROM People WHERE Score <100

© Jim Hope 2004 All Rights Reserved 7 Wildcards SELECT LastName, FirstName FROM People WHERE LastName like 'b*' or MS Access (WHERE LastName like 'b*')

© Jim Hope 2004 All Rights Reserved 8a You can do math? SELECT LastName, FirstName, Score, Score +10 as BigScore FROM People ORDER BY Score DESC

© Jim Hope 2004 All Rights Reserved 9a Create an Alias with CONCAT SELECT CONCAT(LastName,", ",FirstName) AS FullName FROM PEOPLE ORDER BY LastName, FirstName Question: who is [Null]? --- alternate form (MS Access) SELECT LastName +", " + FirstName as FullName FROM People

© Jim Hope 2004 All Rights Reserved 9b Who was NULL? SELECT LastName, FirstName, CONCAT(LastName,", ",FirstName) AS FullName FROM PEOPLE ORDER BY LastName, FirstName This answers the question – who is [NULL] in the previous example

© Jim Hope 2004 All Rights Reserved 10a Max & Min SELECT MAX (Score) FROM People SELECT MIN (Score) FROM People

© Jim Hope 2004 All Rights Reserved 10b Simple Stats and Aliases SELECT MIN(Score) as `Lowest Score`, ROUND(AVG(Score),2) as Average, MAX(Score) as `Highest Score`, ROUND(STD(Score),2) as `Standard Deviation` FROM People

© Jim Hope 2004 All Rights Reserved 10c Limiting Rows Returned SELECT LastName, FirstName, Score FROM People ORDER BY Score DESC LIMIT 1 Who are we missing here?

© Jim Hope 2004 All Rights Reserved 10c Max again SELECT LastName, FirstName, Score FROM People WHERE Score = (SELECT MAX(Score) FROM People); This is a Subquery Version 4.1 Alpha

© Jim Hope 2004 All Rights Reserved 11 Keeping things DISTINCT Try This… SELECT City FROM People Then Try SELECT DISTINCT City FROM People

© Jim Hope 2004 All Rights Reserved 12a More than one table Try SELECT `Team Name` FROM Team Then Try SELECT `Team Name`, LastName, FirstName FROM Team, People This creates a Cartesian Product!

© Jim Hope 2004 All Rights Reserved 12b More than one table SELECT `Team Name`, LastName, FirstName FROM Team, People WHERE Team.Team=People.Team ORDER BY `Team Name`, LastName, FirstName

© Jim Hope 2004 All Rights Reserved 12c Using Join SELECT `Team Name`, LastName, FirstName FROM Team INNER JOIN People ON Team.Team=People.Team ORDER BY `Team Name`, LastName, FirstName Last two lines no longer required FROM People, Team WHERE People.Team=Team.Team

© Jim Hope 2004 All Rights Reserved 12c Using Join SELECT LastName, FirstName, Score, `Show Name` FROM People INNER JOIN `Show` ON People.Show = Show.Show ;

© Jim Hope 2004 All Rights Reserved 12d Using Left Join SELECT LastName, FirstName, Score, `Show Name` FROM People Left JOIN `Show` ON People.Show = Show.Show ;

© Jim Hope 2004 All Rights Reserved 12e Using Right Join SELECT LastName, FirstName, Score, `Show Name` FROM People Right JOIN `Show` ON People.Show = Show.Show ;

© Jim Hope 2004 All Rights Reserved 13a Group By – putting it together SELECT `Team Name`, ROUND(AVG(Score),2) AS `Average` FROM People,Team WHERE People.Team=Team.Team GROUP BY Team.`Team Name`

© Jim Hope 2004 All Rights Reserved 13b Inner Join and Group By SELECT `Team Name`, ROUND(AVG(Score),2) AS `Average` FROM Team INNER JOIN People ON People.Team=Team.Team GROUP BY Team.`Team Name

© Jim Hope 2004 All Rights Reserved 14 SELECT INTO (new table) SELECT Team.`Team Name`, AVG(Score) AS `Average` INTO TeamSummary FROM People INNER JOIN Team ON People.Team = Team.Team GROUP BY Team.`Team Name` Sorry – you don’t have permissions to do this on the netpub instance of MySQL

© Jim Hope 2004 All Rights Reserved 16 SELECT with IF SELECT LastName, IF(FirstName IS NULL, Description, FirstName) as Salutation, Score FROM People, Titles WHERE People.t = Titles.Titles

© Jim Hope 2004 All Rights Reserved 16 SELECT and GROUP BY SELECT `Team Name`, avg(Score) as AvgScore FROM People, Team WHERE People.Team = Team.Team GROUP BY `Team Name`

© Jim Hope 2004 All Rights Reserved That’s enough of that