G64DBS Database Systems. Lecture 7 SQL SELECT. The Data Dictionary. Data Dictionaries. Different Sections of SQL (DDL) Different Sections of SQL (DCL)

Similar documents
In this Lecture. More SQL Data Definition. Deleting Tables. Creating Tables. ALTERing Columns. Changing Tables. More SQL

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

G64DBS Database Systems. Lecture 6 More SQL Data. Creating Relations. Column Constraints. Last Lecture s Question

In This Lecture. SQL Data Definition SQL SQL. Non-Procedural Programming. Notes. Database Systems Lecture 5 Natasha Alechina

Relational Data Structure and Concepts. Structured Query Language (Part 1) The Entity Integrity Rules. Relational Data Structure and Concepts

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Database Management System 9

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

1. Using Bitvise SSH Secure Shell to login to CS Systems Note that if you do not have Bitvise ssh secure shell on your PC, you can download it from

EE221 Databases Practicals Manual

Creating and Managing Tables Schedule: Timing Topic

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

Oracle Database 10g: Introduction to SQL

Oracle Syllabus Course code-r10605 SQL

Chapter 1 SQL and Data

AO3 - Version: 2. Oracle Database 11g SQL

Informatics 1: Data & Analysis

CGS 3066: Spring 2017 SQL Reference

Querying Data with Transact SQL

Chapter 13 : Informatics Practices. Class XI ( As per CBSE Board) SQL Commands. New Syllabus Visit : python.mykvs.in for regular updates

Chapter-14 SQL COMMANDS

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

Section 2.2: Relational Databases

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014

Business Analytics. SQL PL SQL [Oracle 10 g] P r i n c e S e t h i w w w. x l m a c r o. w e b s. c o m

SQL Simple Queries. Chapter 3.1 V3.01. Napier University

Sql Server Syllabus. Overview

SQL Commands & Mongo DB New Syllabus

UNIT-IV (Relational Database Language, PL/SQL)

Microsoft Querying Microsoft SQL Server 2014

Data, Databases, and DBMSs

Relational Database Language

ASSIGNMENT NO 2. Objectives: To understand and demonstrate DDL statements on various SQL objects

Oracle User Administration

SQL - Subqueries and. Schema. Chapter 3.4 V4.0. Napier University

20461: Querying Microsoft SQL Server 2014 Databases

UNIT 4 DATABASE SYSTEM CATALOGUE

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

SQL. History. From Wikipedia, the free encyclopedia.

DB Creation with SQL DDL

G64DBS Database Systems. G64DBS Module. Recommended Textbook. Assessment. Recommended Textbook. Recommended Textbook.

Physical DB Issues, Indexes, Query Optimisation. Database Systems Lecture 13 Natasha Alechina

Deccansoft softwareservices-microsoft Silver Learing Partner. SQL Server Syllabus

Data about data is database Select correct option: True False Partially True None of the Above

Informatics 1: Data & Analysis

20461: Querying Microsoft SQL Server

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

CMPT 354: Database System I. Lecture 3. SQL Basics

RDBMS-Day3. SQL Basic DDL statements DML statements Aggregate functions

Oracle Database 11g: SQL and PL/SQL Fundamentals

COMP 430 Intro. to Database Systems. Encapsulating SQL code

SQL Interview Questions

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies

Database Technology Introduction. Heiko Paulheim

Course Outline and Objectives: Database Programming with SQL

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

Review: Where have we been?

@vmahawar. Agenda Topics Quiz Useful Links

Lecture 1: Relational Databases

Advanced SQL Tribal Data Workshop Joe Nowinski

Oracle Database 12c SQL Fundamentals

Oracle Database: Introduction to SQL Ed 2

Database Languages. A DBMS provides two types of languages: Language for accessing & manipulating the data. Language for defining a database schema

STRUCTURED QUERY LANGUAGE (SQL)

Relation Databases. By- Neha Tyagi PGT CS KV 5 Jaipur II Shift Jaipur Region. Based on CBSE Curriculum Class -11. Neha Tyagi, PGT CS II Shift Jaipur

INTRODUCTION TO DATABASE

CSCB20 Week 4. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

Assignment Session : July-March

Review -Chapter 4. Review -Chapter 5

Introduction to Computer Science and Business

- Database: Shared collection of logically related data and a description of it, designed to meet the information needs of an organization.

Techno India Batanagar Computer Science and Engineering. Model Questions. Subject Name: Database Management System Subject Code: CS 601

Today Learning outcomes LO2

Data Manipulation Language (DML)

Lecture 2: Introduction to SQL

What is SQL? Designed to retrieve data from relational databases, as well as to build and administer those databases.

After completing this course, participants will be able to:

SQL (Structured Query Language)

Querying Microsoft SQL Server (MOC 20461C)

In This Lecture. The Relational Model. The Relational Model. Relational Data Structure. Unnamed and named tuples. New thing:scheme (and attributes)

Chapter 1: Introduction

Oracle Database: Introduction to SQL

IT360: Applied Database Systems. SQL: Structured Query Language DDL and DML (w/o SELECT) (Chapter 7 in Kroenke) SQL: Data Definition Language

5. Single-row function

Microsoft Querying Data with Transact-SQL - Performance Course

SQL Data Query Language

normalization are being violated o Apply the rule of Third Normal Form to resolve a violation in the model

M.C.A. (CBCS) Sem.-III Examination November-2013 CCA-3004 : Database Concepts and Tools. Faculty Code: 003 Subject Code:

Informatics 1: Data & Analysis

Module 9: Managing Schema Objects

Structured Query Language: Introduction

COURSE OUTLINE: Querying Microsoft SQL Server

Oracle. SQL(Structured Query Language) Introduction of DBMS. Build In Function. Introduction of RDBMS. Grouping the Result of a Query

Oracle Database: SQL and PL/SQL Fundamentals NEW

20461D: Querying Microsoft SQL Server

Oracle Database 11g: Introduction to SQLRelease 2

ORACLE VIEWS ORACLE VIEWS. Techgoeasy.com

Querying Microsoft SQL Server

Transcription:

G64DBS Database Systems Lecture 7 SQL SELECT Tim Brailsford Different Sections of SQL (DDL) The Data Definition Language (DDL): CREATE TABLE - creates a new database table ALTER TABLE - alters (changes) a database table DROP TABLE - deletes a database table CREATE INDEX - creates an index (search key) DROP INDEX!- deletes an index Different Sections of SQL (DML) Data Manipulation Language (DML): UPDATE - updates data in a database table DELETE FROM- deletes data from a database table INSERT INTO - inserts new data into a database table SELECT - extracts data from a database table Different Sections of SQL (DCL) Data Control Language (DCL). DESCRIBE list the structure of a table GRANT - gives user's access privileges to database REVOKE - withdraw access given with the GRANT command COMMIT submit a batch of db alterations as a permanant change ROLLBACK rewind a database to a previous state The Data Dictionary Data Dictionaries The data dictionary or catalogue stores Information about database tables Information about the columns of tables Other information - users, locks, indexes, and more This is metadata Some DBMSs let you query the catalogue In Oracle you can access the metadata in several ways There are system tables with metadata in them You can also DESCRIBE tables To find the details of a table use DESCRIBE <table name> Example: SQL> DESCRIBE ; Null? Type ------------ -------- ---------- STU NOT NULL NUMBER(38) STUNAME NOT NULL VARCHAR2(50) STUADDRESS VARCHAR2(50) STUYEAR NUMBER(38)

Oracle Data Dictionary SELECT To find out what tables and sequences you have defined use: SELECT table_name FROM user_tables The user_tables table is maintained by Oracle It has lots of columns, so don t use user_tables The SQL command you will use most often Queries a set of tables and returns results as a table Lots of options, we will look at many of them Usually more than one way to do any given query SQL s SELECT is different from the relational algebra s Select SELECT in SQL does all of the relational algebra But it is a bit different because SQL differs from the relational model SQL SELECT Overview SELECT [DISTINCT ALL] <column-list> FROM <table-names> [WHERE <condition>] [ORDER BY <column-list>] [GROUP BY <column-list>] [HAVING <condition>] First Last S103 John Smith S104 Mary Jones S105 Jane Brown S106 Mark Jones S107 John Brown Example Course Code Title DBS Database Systems PR1 Programming 1 PR2 Programming 2 IAI Intro to AI Grade Code Mark S103 IAI 58 S104 IAI 65 S106 PR2 43 S107 PR1 76 S107 PR2 60 S107 IAI 35 ([]- optional, - or) Sample SELECTs SELECT FROM Sample SELECTs S103 S104 S105 S106 S107 First Last S103 John Smith S104 Mary Jones S105 Jane Brown S106 Mark Jones S107 John Brown

DISTINCT and ALL WHERE Clauses Sometimes you end up with duplicate entries Using DISTINCT removes duplicates Using ALL retains them - this is the default SELECT ALL Last FROM SELECT DISTINCT Last FROM Last Smith Jones Brown Jones Brown Last Smith Jones Brown Usually you don t want all the rows A WHERE clause restricts the rows that are returned It takes the form of a condition - only those rows that satisfy the condition are returned Example conditions: Mark < 40 First = John First <> John First = Last (First = John ) AND (Last = Smith ) (Mark < 40) OR (Mark > 70) WHERE Examples Grade WHERE Mark >= 60 Code Mark S104 IAI 65 S107 PR1 76 S107 PR2 60 SELECT DISTINCT FROM Grade WHERE Mark >= 60 S103 S104 S107 WHERE Example Code Mark S103 IAI 58 S104 Given the IAItable 65 S106 IAI 43 S107 PR2 76 S107 PR1 60 S107 PR2 35 Write an SQL query to find a list of the numbers and marks in IAI of students who have passed (scored 40 or higher) IAI Mar S10 58 k S10 3 65 4 One Solution We only want the and Mark, not the Code Single quotes around t string SELECT, Mark FROM Grade WHERE (Code = IAI ) AND (Mark >= 40) We re only interested in IAI We re looking for entries with pass marks Often you need to combine information from two or more tables You can get the effect of a product by using: Table1, Table2... If the tables have columns with the same name ambiguity results You resolve this by referencing columns with the table name Table.Column

SELECT First, Last, Mark FROM, Grade WHERE (. = Grade.) AND (Mark >= 40) First Last S103 John Smith S104 Mary Jones Grade S105 Jane Brown S106 Mark Jones Code S107 Mark John Brown S103 IAI 58 S104 IAI 65 S106 PR2 43 S107 PR1 76 S107 PR2 60 S107 IAI 35 Are matched with the first entry from the table... And then with the second SELECT... FROM, Grade WHERE... First Last Code Mark S103 John Smith S103 John Smith S103 IAI 58 S103 John Smith S103 John Smith S104 IAI 65 S103 John Smith S106 PR2 43 S103 John Smith S107 PR1 76 S103 John Smith S107 PR2 60 S103 John Smith S107 IAI 35 S104 Mary Jones S104 Mary Jones S103 IAI 58 S104 Mary Jones S104 Mary Jones S104 IAI 65 S104 Mary Jones S 43 All of the entries from the Grade table and so on SELECT... FROM, Grade WHERE (. = Grade.) AND... SELECT... FROM, Grade WHERE (. = Grade.) AND (Mark >= 40) First Last Code Mark S103 John Smith S103 John Smith S103 IAI 58 S104 Mary Jones S104 Mary Jones S104 IAI 65 S106 Mark Jones S106 PR2 43 S107 John Brown S107 PR1 76 S107 John Brown S107 PR2 60 S107 John Brown S107 IAI 35 First Last Code Mark S103 John Smith S103 John Smith S103 IAI 58 S104 Mary Jones S104 Mary Jones S104 IAI 65 S106 Mark Jones S106 PR2 43 S107 John Brown S107 PR1 76 S107 John Brown S107 PR2 60. Grade. SELECT First, Last, Mark FROM, Grade WHERE (. = Grade.) AND (Mark >= 40) First Last Mark John Smith 72 John Smith 58 Mary Jones 68 Mary Jones 65 Mark Jones 43 John Brown 76 John Brown 60 When selecting from multiple tables you almost always use a WHERE clause to find entries with common values You have joined tables by listing them with commas., Grade, Course WHERE. = Grade. AND Course.Code = Grade.Code

First Last Code Mark Code Title Course S103 John Smith DBS Database Systems S103 John Smith S103 IAI 58 IAI Intro to AI S104 Mary Jones PR1 Programming 1 S104 Mary Jones S104 IAI 65 IAI Intro to AI S106 Mark Jones S106 PR2 43 PR2 Programming 2 S107 John Brown S107 PR1 76 PR1 Programming 1 S107 John Brown S107 PR2 60 PR2 Programming 2 S107 John Brown S107 IAI 35 IAI Intro to AI JOINs can be used to combine tables There are many types of JOIN CROSS JOIN NATURAL JOIN OUTER JOIN JOINs A CROSS JOIN B returns all pairs of rows from A and B A NATURAL JOIN B returns pairs of rows with common values A B returns pairs of rows satisfying a condition. = Grade. Course.Code = Grade.Code OUTER JOINs are linked with NULLs - more later CROSS JOIN NATURAL JOIN 123 John 124 Mary 125 Mark 126 Jane Code 123 DBS 124 PRG 124 DBS 126 PRG CROSS JOIN Code 123 John 123 DBS 124 Mary 123 DBS 125 Mark 123 DBS 126 Jane 123 DBS 123 John 124 PRG 124 Mary 124 PRG 125 Mark 124 PRG 126 Jane 124 PRG 123 John 124 DBS 124 Mary DBS 123 John 124 Mary 125 Mark 126 Jane Code 123 DBS 124 PRG 124 DBS 126 PRG NATURAL JOIN Code 123 John 123 DBS 124 Mary 124 PRG 124 Mary 124 DBS 126 Jane 126 PRG Equivalencies A CROSS JOIN B is the same as A, B A NATURAL JOIN B B is the same as A, B WHERE A.col1 = B.col1 AND A.col2 = B.col2 AND... s specify a condition which the pairs of rows satisfy A B ON <condition> Can also use A B USING (col1, col2, ) Chooses rows where the given columns are equal

Buyer Budget Smith 100,000 Jones 150,000 Green 80,000 Buyer Property ON Price <= Budget Buyer Budget Smith 100,000 Jones 150,000 Green 80,000 Buyer Property ON Price <= Budget Property Address Price 15 High St 85,000 12 Queen St 125,000 87 Oak Row 175,000 Budget Address Price Smith 100,000 15 High St 85,000 Jones 150,000 15 High St 85,000 Jones 150,000 12 Queen St 125,000 Property Address Price 15 High St 85,000 12 Queen St 125,000 87 Oak Row 175,000 Budget Address Price Smith 100,000 15 High St 85,000 Jones 150,000 15 High St 85,000 Jones 150,000 12 Queen St 125,000 A B ON <condition> is the same as A, B WHERE <condition> A B USING(col1, col2,...) is the same as A, B WHERE A.col1 = B.col1 AND A.col2 = B.col2 AND... JOINs vs WHERE Clauses JOINs (so far) are not needed You can have the same effect by selecting from multiple tables with an appropriate WHERE clause So should you use JOINs or not? Yes, because They lead to better, conciser queries NATURAL JOINs are very common No, because Support for JOINs varies a fair bit among SQL dialects it can be inconsistent. very common Writing Queries When writing queries There are often many ways to write the query You should worry about being correct, clear, and concise in that order Don t worry about being clever or efficient Most DBMSs have query optimisers These take a users query and figure out how to efficiently execute it A simple query is easier to optimise We ll look at some ways to improve efficiency later