Copyright 2003 Curt Hill Queries in SQL Syntax and semantics.

Slides:



Advertisements
Similar presentations
IS698: Database Management Min Song IS NJIT. The Relational Data Model.
Advertisements

Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
Relational Model (CB Chapter 4) CPSC 356 Database Ellen Walker Hiram College.
From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Chapter 14 Getting to First Base: Introduction to Database Concepts.
Chapter 6 SQL: Data Manipulation Cont’d. 2 ANY and ALL u ANY and ALL used with subqueries that produce single column of numbers u ALL –Condition only.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
Microsoft Access 2010 Chapter 7 Using SQL.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
1 Announcements Read 6.7 – 6.10 for Friday Homework 6, due Friday 10/29 Research paper –List of sources - due 10/29 Department Seminar –The Role of Experimentation.
Copyright © Curt Hill SQL The Data Definition Language.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
Concepts of Database Management, Fifth Edition
Chapter 2 Basic SQL SELECT Statements
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Chapter 2 Basic SQL SELECT Statements Oracle 10g: SQL.
Chapter 3 Section 3.4 Relational Database Operators
Copyright © Curt Hill The Relational Algebra What operations can be done?
Copyright © Curt Hill Index Creation SQL.
SQL Joins Oracle and ANSI Standard SQL Lecture 6.
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
Copyright © Curt Hill The Relational Model of Database Basic organization and terms.
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language.
Copyright © Curt Hill Query Evaluation Translating a query into action.
Copyright Curt Hill SQL Queries Yet Another Set of Query Features.
2 Writing Basic SELECT Statements. 1-2 Copyright  Oracle Corporation, All rights reserved. Capabilities of SQL SELECT Statements Selection Projection.
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
Copyright © Curt Hill SQL The Intergalactic Standard Database Query Language.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Relational Databases.  In week 1 we looked at the concept of a key, the primary key is a column/attribute that uniquely identifies the rest of the data.
BACS 287 Structured Query Language 1. BACS 287 Visual Basic Table Access Visual Basic provides 2 mechanisms to access data in tables: – Record-at-a-time.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
Copyright © Curt Hill Queries in SQL More options.
Copyright © Curt Hill Joins Revisited What is there beyond Natural Joins?
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Part a The Relational Algebra and Relational Calculus Hours 1,2.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Copyright © Curt Hill The Relational Calculus Another way to do queries.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
1 Chapter 2 Basic SQL SELECT Statements. 2 Chapter Objectives Distinguish between an RDBMS and an ORDBMS Identify keywords, mandatory clauses, and optional.
SQL advanced select using Oracle 1 Multiple Tables: Joins and Set Operations Subqueries: Nested Queries.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Copyright © Curt Hill SQL The Data Manipulation Language.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Retrieving Data Using the SQL SELECT Statement
CS4432: Database Systems II
Writing Basic SQL SELECT Statements
Retrieving Data Using the SQL SELECT Statement
A poorly named Curt Hill utility program
CS4222 Principles of Database System
Retrieving Data Using the SQL SELECT Statement
Structured Query Language – The Basics
Join Implementation How is it done? Copyright © Curt Hill.
Presentation transcript:

Copyright 2003 Curt Hill Queries in SQL Syntax and semantics

Copyright 2003 Curt Hill Select is the basic query command The most common form is: SELECT field1, field2…fieldn FROM table1,table2…tablem WHERE condition Select f.name, c.dept, c.number, c.crhr From faculty as f, course as c, faculty_teach as ft Where f.naid=ft.naid AND ft.dept = c.dept AND ft.number = c.number

Copyright 2003 Curt Hill Syntax The statement is free form –May be on one line or many –Words, constants and strings must be complete on one line Punctuation is minimal –Commas in lists My prefererance –Select, From, Where on separate lines –One condition per line

Copyright 2003 Curt Hill Three pieces Select –Describes the resulting table From –Lists the tables that will be used to generate the final table Where –Gives the conditions or comparisons Join fields Selection comparisons –Where is optional

Copyright 2003 Curt Hill Select List fields desired This may include constants and calculations Field names may be qualified by table names if needed Corresponds to the project of algebra Use * to get all fields Many clauses to be discussed later

Copyright 2003 Curt Hill From Specify the tables Allows renaming the table for convenience in the rest of the query Form is: FROM Tab1, Tab2 AS t, Tab3 u –The AS reserved word renames for the rest of the statement –May be left out Only tables mentioned here may be accessed

Copyright 2003 Curt Hill Example Without Where Select name, degree From Faculty Select * From Faculty, Faculty_Teach –Cartesian Product

Copyright 2003 Curt Hill Where Supply conditions Compare the field name with a value –Does the selection part Compare two fields from different tables –Does the join Is actually optional but leaving it out gives the whole table or a cartesian product –Not a join

Copyright 2003 Curt Hill Where Comparisons Comparison operators –= –> –< –>= –<= –<>

Copyright 2003 Curt Hill Where boolean operators And Or Not ()

Copyright 2003 Curt Hill Student Score Example Select name, dept, course, score From Students, Grades Where Students.naid=Grades.naid Naid need qualification, since it exists in both, rest are unique From allows rename or synonyms Select name, dept, course, score From Students s, Grades as g Where S.naid=G.naid

Copyright 2003 Curt Hill Course a faculty member teaches Select name, ft.dept, course from faculty, faculty_teach ft where faculty.naid = ft.naid Put the credit hours in: Select name, ft.dept, course, crhr from faculty, faculty_teach ft, course c where faculty.naid = ft.naid AND ft.dept=c.dept AND ft.number = c.number

Copyright 2003 Curt Hill All students who got a B or better in any CS class Select name From grades, students Where score>=80 and students.naid = grades.naid And dept = ‘CS’

Copyright 2003 Curt Hill Syntax Again Reserved words and names are not sensitive to case Blanks, tabs, line feeds and other white space are ignored Layout only affects readability The order of things following Select determines the table order The order of tables following From does not matter The order of conditions does not matter

Copyright 2003 Curt Hill Designing Queries Start with the From clause –What tables are needed –Will duplicates of these be needed Make the Where next –Consider joins first –Consider selection next Finally design the Select –This is the final projection that shows the fields that you will see

Copyright 2003 Curt Hill Considering Joins How are tables connected? Two tables can be joined on any sets of fields provided: –The number and type of each set matches Names do not need to match The types are defined in the create table statement Especially look at foreign keys –Items in one that are primary in the other –Most joins are equijoins on a foreign key and primary key

Copyright 2003 Curt Hill Find all students that each faculty member teaches Select f.name, s.name from faculty as f, students s, faculty_teach ft, grades g where s.naid = g.naid AND f.naid = ft.naid And ft.dept = g.dept AND ft.number = g.number

Copyright 2003 Curt Hill Faculty members and their department and divisional chair Multiple copies are needed for multiple names Select f.name, dp.name, dv.name from faculty as f, faculty dp, faculty dv, departments dept, division div where f.dept = dept.dept AND dept.chair = dp.naid And dept.division = div.division AND div.chair = dv.naid

Copyright 2003 Curt Hill More options A select usually joins several tables creating large unique tuples The select only shows some of these fields –A projection on the larger tuple –This projection does not eliminate duplicates We now get a choice to retain or eliminate duplicates –Reserved words ALL and DISTINCT

Copyright 2003 Curt Hill All students who got a B or better in any CS class Select distinct name From grades, students Where score>=80 and students.naid = grades.naid