Introduction to SQL. Tara Murphy and James Curran. 15th April, 2009

Similar documents
Outline. Introduction to SQL. What happens when you run an SQL query? There are 6 possible clauses in a select statement. Tara Murphy and James Curran

Relational Database Management Systems for Epidemiologists: SQL Part II

Querying Data with Transact SQL

T-SQL Training: T-SQL for SQL Server for Developers

CSC Web Programming. Introduction to SQL

More MySQL ELEVEN Walkthrough examples Walkthrough 1: Bulk loading SESSION

Based on the following Table(s), Write down the queries as indicated: 1. Write an SQL query to insert a new row in table Dept with values: 4, Prog, MO

Relational Database Management Systems for Epidemiologists: SQL Part I

MariaDB Crash Course. A Addison-Wesley. Ben Forta. Upper Saddle River, NJ Boston. Indianapolis. Singapore Mexico City. Cape Town Sydney.

CGS 3066: Spring 2017 SQL Reference

Databases (MariaDB/MySQL) CS401, Fall 2015

Database design process

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

Basic SQL. Basic SQL. Basic SQL

Querying Data with Transact-SQL

Data Manipulation Language (DML)

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

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Sql Server Syllabus. Overview

SQL functions fit into two broad categories: Data definition language Data manipulation language

The SELECT-FROM-WHERE Structure

Database Technology. Topic 3: SQL. Olaf Hartig.

Unit Assessment Guide

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

The SELECT-FROM-WHERE Structure

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

CS2 Current Technologies Lecture 2: SQL Programming Basics

12. MS Access Tables, Relationships, and Queries

Querying Data with Transact-SQL

EE221 Databases Practicals Manual

Workbooks (File) and Worksheet Handling

SQL Part 2. Kathleen Durant PhD Northeastern University CS3200 Lesson 6

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus Lecture #17: MySQL Gets Done

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

Advanced SQL Tribal Data Workshop Joe Nowinski

CS 327E Lecture 2. Shirley Cohen. January 27, 2016

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

The SQL Guide to Pervasive PSQL. Rick F. van der Lans

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

Oracle Syllabus Course code-r10605 SQL

Language. f SQL. Larry Rockoff COURSE TECHNOLOGY. Kingdom United States. Course Technology PTR. A part ofcenqaqe Learninq

Provider: MySQLAB Web page:

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

1. Data Definition Language.

Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

1Z0-071 Exam Questions Demo Oracle. Exam Questions 1Z Oracle Database 12c SQL.

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

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

Why Relational Databases? Relational databases allow for the storage and analysis of large amounts of data.

MTA Database Administrator Fundamentals Course

1Z0-071 Exam Questions Demo Oracle. Exam Questions 1Z Oracle Database 12c SQL.

Handout 9 CS-605 Spring 18 Page 1 of 8. Handout 9. SQL Select -- Multi Table Queries. Joins and Nested Subqueries.

COSC 304 Introduction to Database Systems SQL. Dr. Ramon Lawrence University of British Columbia Okanagan

SQL - Data Query language

Querying Data with Transact-SQL (20761)

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

Visit for more.

More SQL: Complex Queries, Triggers, Views, and Schema Modification

Querying Microsoft SQL Server 2014

Querying Data with Transact-SQL

COURSE OUTLINE MOC 20461: QUERYING MICROSOFT SQL SERVER 2014

Aggregate Functions. Eng. Mohammed Alokshiya. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Dept. Database Lab (ECOM 4113)

Computing for Medicine (C4M) Seminar 3: Databases. Michelle Craig Associate Professor, Teaching Stream

[AVNICF-MCSASQL2012]: NICF - Microsoft Certified Solutions Associate (MCSA): SQL Server 2012

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

Midterm Review. Winter Lecture 13

Instructor: Craig Duckett. Lecture 03: Tuesday, April 3, 2018 SQL Sorting, Aggregates and Joining Tables

Operating systems fundamentals - B07

COMP 244 DATABASE CONCEPTS & APPLICATIONS

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Chapter-14 SQL COMMANDS

1) Introduction to SQL

618 Index. BIT data type, 108, 109 BIT_LENGTH, 595f BIT VARYING data type, 108 BLOB data type, 108 Boolean data type, 109

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

20461: Querying Microsoft SQL Server 2014 Databases

Database Management Systems,

CSCU9Q5 Introduction to MySQL. Data Definition & Manipulation (Over ~two Lectures)

Chapter 3. Introduction to relational databases and MySQL. 2010, Mike Murach & Associates, Inc. Murach's PHP and MySQL, C3

Oracle 1Z0-882 Exam. Volume: 100 Questions. Question No: 1 Consider the table structure shown by this output: Mysql> desc city:

20461: Querying Microsoft SQL Server

Intermediate SQL: Aggregated Data, Joins and Set Operators

Exact Numeric Data Types

After completing this course, participants will be able to:

Querying Microsoft SQL Server (MOC 20461C)

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Administrivia. Administrivia. Faloutsos/Pavlo CMU /615

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

SQL Data Query Language

Advanced MySQL Query Tuning

Database Technology. Topic 2: Relational Databases and SQL. Olaf Hartig.

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

Follow these steps to get started: o Launch MS Access from your start menu. The MS Access startup panel is displayed:

Lecture 6 - More SQL

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Lecture 06. Fall 2018 Borough of Manhattan Community College

Real-World Performance Training SQL Introduction

WEEK 3 TERADATA EXERCISES GUIDE

Module 3 MySQL Database. Database Management System

Structured Query Language Continued. Rose-Hulman Institute of Technology Curt Clifton

SQL Interview Questions

Transcription:

Introduction to SQL Tara Murphy and James Curran 15th April, 2009

Basic SQL queries Filtering Joining tables Grouping 2 What happens when you run an SQL query? ˆ To run an SQL query the following steps occur 1 You connect to the server which verifies your username and password 2 You send a query to the server 3 The server checks that ˆ You have permission to execute the statement ˆ You have permission to access the data ˆ The statement syntax is correct 4 Then your query is passed to the query optimizer which determines the most efficient way to execute the query 5 The optimizer chooses an execution plan which is used by the server to execute your query 6 Finally the result set is returned to the original application client

Basic SQL queries Filtering Joining tables Grouping 3 There are 6 possible clauses in a select statement In MySQL only one clause is mandatory (select) however most queries will consist of a few clauses select determines which columns to include in the result set from identifies the tables to query and how to join them where restricts the number of rows in the result set by applying a condition group by groups rows together using common column values having restricts the number of rows in the result set using grouped data order by sorts the rows of the result set

Basic SQL queries Filtering Joining tables Grouping 4 Case study: AT20G Bright Source Sample ˆ For the rest of this lecture we will be doing queries on a database I have set up using AT20G Survey Data ˆ The database contains the following tables bss Position and flux data for 800 sources observed at 20 GHz with the ATCA. These are the Bright Source Sample (BSS). at 20 GHz with the ATCA. duplicates Information about sources that were observed multiple times.

Basic SQL queries Filtering Joining tables Grouping 5 describe shows the structure of a table We can inspect any of the tables in the database using DESC 1 mysql> DESC bss; +-----------+---------------------+------+-----+---------+ Field Type Null Key Default +-----------+---------------------+------+-----+---------+ name varchar(30) NO PRI obs varchar(15) YES NULL ra double YES NULL decl double YES NULL... f tinyint(3) unsigned YES NULL run varchar(10) YES NULL +-----------+---------------------+------+-----+---------+ 17 rows in set (0.01 sec)

Basic SQL queries Filtering Joining tables Grouping 6 select chooses columns from a table ˆ Show me all columns from the duplicate table 1 mysql> SELECT * 2 -> FROM duplicate; +----------------------+---------+----------------------+--------+--------+-----------+ name1 run1 name2 run2 offset type +------------ ---------+---------+----------------------+--------+--------+-----------+ AT20G J004426-842239 apr06 AT20G J004426-842242 oct05 2.8 Duplicate AT20G J010915-604948 oct05 AT20G J010915-604948 oct04 1.51 Duplicate AT20G J010915-604948 oct05 AT20G J010915-604948 apr06 0.44 Duplicate... +------------ ---------+---------+----------------------+--------+--------+-----------+ 172 rows in set (0.01 sec)

Basic SQL queries Filtering Joining tables Grouping 7 select chooses columns from a table ˆ Show me the name1 column from the duplicate table 1 mysql> SELECT name1 2 -> FROM duplicate; +---------------------------+ name1 +---------------------------+ AT20G J004426.05-842239.3 AT20G J010915.40-604948.6 AT20G J010915.40-604948.6 AT20G J010915.46-604948.5... +---------------------------+ 172 rows in set (0.00 sec)

Basic SQL queries Filtering Joining tables Grouping 8 distinct removes duplicates from your results ˆ You want to retrieve a list of run names 1 mysql> SELECT run1 2 -> FROM duplicate; +---------+ run1 +---------+ apr06 oct05 oct05 apr06 apr06... +---------+ 172 rows in set (0.00 sec)

Basic SQL queries Filtering Joining tables Grouping 9 distinct removes duplicates from your results 1 mysql> SELECT DISTINCT run1 2 -> FROM duplicate; +---------+ run1 +---------+ apr06 oct05 oct04 oct06 may07gt may07k +---------+ 6 rows in set (0.00 sec) Warning: distinct requires sorting and can be slow on large queries.

Basic SQL queries Filtering Joining tables Grouping 10 The where clause filters out unwanted rows ˆ You want to retrieve information about all sources observed in oct04 1 mysql> SELECT name, ra, dec, run 2 -> FROM bss 3 -> WHERE run = 'oct04'; +---------------------------+----------+----------+-------+ name ra decl run +---------------------------+----------+----------+-------+ AT20G J000435.65-473619.1 1.1485-47.6053 oct04 AT20G J001035.92-302748.3 2.6497-30.4634 oct04 AT20G J001259.89-395426.4 3.2495-39.9073 oct04... +---------------------------+----------+----------+-------+ 148 rows in set (0.01 sec)

Basic SQL queries Filtering Joining tables Grouping 11 You can combine multiple filtering conditions ˆ Conditional expressions: AND, OR or NOT ˆ Comparison operators (=,! =, <, >, <>) ˆ You want to retrieve information about all sources observed in the oct04 run, South of 70 declination. 1 mysql> SELECT name, ra, decl, run 2 -> FROM bss 3 -> WHERE (run = 'oct04') AND (decl < -65); +---------------------------+----------+----------+-------+ name ra decl run +---------------------------+----------+----------+-------+ AT20G J040820.33-654508.6 62.0847-65.7524 oct04 AT20G J064428.14-671257.6 101.1173-67.216 oct04 AT20G J074331.60-672625.8 115.8817-67.4405 oct04... +---------------------------+----------+----------+-------+ Tara Murphy and14 Jamesrows Curran inintroduction set (0.00 to SQL sec) 15th April, 2009

Basic SQL queries Filtering Joining tables Grouping 12 You can sort and limit results Find the 5 pairs of duplicates with the greatest offsets 1 mysql> SELECT name1, name2, offset 2 -> FROM duplicate 3 -> ORDER BY offset desc 4 -> LIMIT 5; +----------------------+----------------------+--------+ name1 name2 offset +----------------------+----------------------+--------+ AT20G J132649-525623 AT20G J132649-525535 47.99 AT20G J142431-491301 AT20G J142432-491349 47.75 AT20G J142741-330531 AT20G J142743-330527 26.5 AT20G J145427-374733 AT20G J145429-374728 24.86 AT20G J142756-420618 AT20G J142758-420616 24.57 +----------------------+----------------------+--------+ 5 rows in set (0.00 sec)

Basic SQL queries Filtering Joining tables Grouping 13 Information from multiple tables requires a join ˆ You often want to find information that is split across several tables. ˆ For example, we want to find the RA and Dec of all sources classified as sidelobes. ˆ This requires information from two tables bss and duplicate. 1 mysql> SELECT bss.ra, bss.decl, duplicate.type 2 -> FROM bss, duplicate 3 -> WHERE duplicate.type = 'Sidelobe'; ˆ This results in a cross join, showing every permutation of bss and duplicate (824 2 = 1648 rows)

Basic SQL queries Filtering Joining tables Grouping 14 An inner join is the most common type of join ˆ To correct the previous query we need to make use of the foreign key to link the tables ˆ These tables are linked by the foreign key name. 1 mysql> SELECT bss.ra, bss.decl, duplicate.type 2 -> FROM bss JOIN duplicate 3 -> ON bss.name = duplicate.name1 4 -> WHERE duplicate.type = 'Sidelobe'; ˆ As there are other types of joins, you should specify INNER JOIN rather than just JOIN

Basic SQL queries Filtering Joining tables Grouping 15 Results from the inner join 1 mysql> SELECT bss.ra, bss.decl, duplicate.type 2 -> FROM bss JOIN duplicate 3 -> ON bss.name = duplicate.name1 4 -> WHERE duplicate.type = 'Sidelobe'; +----------+----------+----------+ ra decl type +----------+----------+----------+ 216.1343-49.2304 Sidelobe +----------+----------+----------+ 1 row in set (0.00 sec)

Basic SQL queries Filtering Joining tables Grouping 16 Using table aliases makes the query shorter ˆ When referring to more that one table it is necessary to prefix the column name with the table name to remove ambiguity. ˆ Hence we refer to them as employee.dept_id, department.dept_id ˆ It is more convenient to assign a table alias in the FROM clause: 1 mysql> SELECT b.ra, b.decl, d.type 2 -> FROM bss b 3 -> JOIN duplicate d ON b.name = d.name1;

Basic SQL queries Filtering Joining tables Grouping 17 You should use SQL92 standard syntax ˆ This is a standard that all the major DBMS follow ˆ You should use it for portability across systems ˆ However you should also be aware of pre-sql92 syntax for joins as it is still commonly used 1 mysql> SELECT b.ra, b.decl, d.type 2 -> FROM bss b, duplicate d 3 -> WHERE b.name = d.name1;

Basic SQL queries Filtering Joining tables Grouping 18 Grouping is good for finding trends in your data ˆ We want to find the number of sources observed in each run 1 mysql> SELECT run, COUNT(*) n_sources 2 -> FROM bss 3 -> GROUP BY run; +---------+-----------+ run n_sources +---------+-----------+ apr06 14 may07gt 24 may07k 449 oct04 148 oct05 59 oct06 130 +---------+-----------+ 6 rows in set (0.00 sec)

Basic SQL queries Filtering Joining tables Grouping 19 Group filter conditions are specified using having ˆ The HAVING clause lets you apply filters to groups as follows 1 mysql> SELECT run, COUNT(*) n_sources 2 -> FROM bss 3 -> GROUP BY run 4 -> HAVING n_sources 100; +--------+-----------+ run n_sources +--------+-----------+ may07k 449 oct04 148 oct06 130 +--------+-----------+ 3 rows in set (0.00 sec)

Basic SQL queries Filtering Joining tables Grouping 20 Order is important when combining filters Image Ref: Kifer, Bernstein and Lewis, Database Systems

Basic SQL queries Filtering Joining tables Grouping 21 Aggregation functions work on groups ˆ Aggregation functions perform a specific operation over all rows in a group ˆ MySQL supports the following aggregation functions max() returns the maximum value within a set min() returns the minimum value within a set avg() returns the average value within a set sum() returns the sum of values within a set count() returns the number of values within a set ˆ We have already used count(*) in previous examples

Basic SQL queries Filtering Joining tables Grouping 22 Tutorial options ˆ Option 1: Astronomy SQL ˆ Try SQL questions on SDSS and OpenSkyQuery sites ˆ Option 2: SQL tutorial ˆ If you have MySQL installed, try walkthrough tutorial ˆ Option 3: MySQL tutorial ˆ If you have MySQL installed, try MySQL walkthrough

Basic SQL queries Filtering Joining tables Grouping 23 References ˆ Beaulieu, 2005, Learning SQL, O Reilly