SQL Basics. Introduction to Standard Query Language



Similar documents
Oracle Database: SQL and PL/SQL Fundamentals

Oracle SQL. Course Summary. Duration. Objectives

Oracle Database: SQL and PL/SQL Fundamentals NEW

Database Query 1: SQL Basics

Introduction to Microsoft Jet SQL

Oracle Database: SQL and PL/SQL Fundamentals

Oracle 10g PL/SQL Training

David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design and Implementation

Relational Database: Additional Operations on Relations; SQL

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Financial Data Access with SQL, Excel & VBA

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Oracle Database 12c: Introduction to SQL Ed 1.1

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Oracle Database 10g: Introduction to SQL

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

3.GETTING STARTED WITH ORACLE8i

Introduction to SQL and SQL in R. LISA Short Courses Xinran Hu

Using AND in a Query: Step 1: Open Query Design


Database Administration with MySQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

SQL. Short introduction

Using Multiple Operations. Implementing Table Operations Using Structured Query Language (SQL)

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

P_Id LastName FirstName Address City 1 Kumari Mounitha VPura Bangalore 2 Kumar Pranav Yelhanka Bangalore 3 Gubbi Sharan Hebbal Tumkur

Oracle Database: Introduction to SQL

Oracle Database: Introduction to SQL

SQL Programming. Student Workbook

Appendix A Practices and Solutions

Programming with SQL

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

A basic create statement for a simple student table would look like the following.

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

Ad Hoc Advanced Table of Contents

SQL SELECT Query: Intermediate

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

ATTACHMENT 6 SQL Server 2012 Programming Standards

Using SQL Queries in Crystal Reports

Performing Queries Using PROC SQL (1)

Inquiry Formulas. student guide

Welcome to the topic on queries in SAP Business One.

Advanced Online Media Dr. Cindy Royal Texas State University - San Marcos School of Journalism and Mass Communication

SQL Server 2008 Core Skills. Gary Young 2011

Structured Query Language. Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics

Structured Query Language (SQL)

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

Oracle Database: Introduction to SQL

Access Queries (Office 2003)

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

Agenda. 1. Tom Rosenburg, General Motors Maximizing Ad Hoc Reporting with the Application Where Clause

Introduction to the Oracle DBMS

Effective Use of SQL in SAS Programming

Instant SQL Programming

Netezza SQL Class Outline

9.1 SAS. SQL Query Window. User s Guide

Information Systems SQL. Nikolaj Popov

Chapter 1 Overview of the SQL Procedure

Oracle Database 11g SQL

Oracle. Brief Course Content This course can be done in modular form as per the detail below. ORA-1 Oracle Database 10g: SQL 4 Weeks 4000/-

Query-by-Example (QBE)

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

Database Applications Microsoft Access

Exploring Microsoft Office Access Chapter 2: Relational Databases and Multi-Table Queries

IT2305 Database Systems I (Compulsory)

Creating QBE Queries in Microsoft SQL Server

A Brief Introduction to MySQL

The software shall provide the necessary tools to allow a user to create a Dashboard based on the queries created.

Examine the structure of the EMPLOYEES table: EMPLOYEE_ID NUMBER Primary Key FIRST_NAME VARCHAR2(25) LAST_NAME VARCHAR2(25)

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

How To Create A Table In Sql (Ahem)

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

Database Programming with PL/SQL: Learning Objectives

T-SQL STANDARD ELEMENTS

Unit 10: Microsoft Access Queries

SQL Examples from Chapters 6&7:

Foundations & Fundamentals. A PROC SQL Primer. Matt Taylor, Carolina Analytical Consulting, LLC, Charlotte, NC

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

PSU SQL: Introduction. SQL: Introduction. Relational Databases. Activity 1 Examining Tables and Diagrams

IT2304: Database Systems 1 (DBS 1)

4. The Third Stage In Designing A Database Is When We Analyze Our Tables More Closely And Create A Between Tables

1.204 Lecture 3. SQL: Basics, Joins SQL

Information Technology NVEQ Level 2 Class X IT207-NQ2012-Database Development (Basic) Student s Handbook

How do I view and download reports?

Toad for Data Analysts, Tips n Tricks

Access 2010: Creating Queries Table of Contents INTRODUCTION TO QUERIES... 2 QUERY JOINS... 2 INNER JOINS... 3 OUTER JOINS...

REP200 Using Query Manager to Create Ad Hoc Queries

Beginning C# 5.0. Databases. Vidya Vrat Agarwal. Second Edition

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

Microsoft Excel 2007 Mini Skills Overview of Tables

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

Object-Oriented Query Languages: Object Query Language (OQL)

Using the SQL Procedure

ORACLE 10g Lab Guide

Query. Training and Participation Guide Financials 9.2

SQL Server Database Coding Standards and Guidelines

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

Introduction to SQL and database objects

Microsoft Excel 2010 Part 3: Advanced Excel

Transcription:

SQL Basics Introduction to Standard Query Language

SQL What Is It? Structured Query Language Common Language For Variety of Databases ANSI Standard BUT. Two Types of SQL DML Data Manipulation Language (SELECT) DDL Data Definition Language (CREATE TABLE)

Where To Use SQL*Plus TOAD SQL Navigator ODBC Supported Connections Excel Access Lotus 1-2-31 Heart of PL/SQL

Pros & Cons of SQL Pros: Very flexible Universal (Oracle, Access, Paradox, etc) Relatively Few Commands to Learn Cons: Requires Detailed Knowledge of the Structure of the Database Can Provide Misleading Results

Basic SQL Components SELECT schema.table.column column FROM table alias WHERE [conditions] ORDER BY [columns] ; Defines the end of an SQL statement Some programs require it, some do not (TOAD Does Not) Needed only if multiple SQL statements run in a script Optional Elements

SELECT Statement SELECT Statement Defines WHAT is to be returned (separated by commas) Database Columns (From Tables or Views) Constant Text Values Formulas Pre-defined Functions Group Functions (COUNT, SUM, MAX, MIN, AVG) * Mean All Columns From All Tables In the FROM Statement Example: SELECT state_code, state_name

FROM Statement Defines the Table(s) or View(s) Used by the SELECT or WHERE Statements You MUST Have a FROM statement Multiple Tables/Views are separated by Commas

Examples SELECT state_name, state_abbr FROM states SELECT * FROM agencies SELECT arithmetic_mean minimum_value SELECT FROM annual_summaries

WHERE Clause Optional Defines what records are to be included in the query Uses Conditional Operators =, >, >=, <, <=,!= (<>) BETWEEN x AND y IN ( (list) LIKE %string ( % is a wild-card) IS NULL NOT {BETWEEN / IN / LIKE / NULL} Multiple Conditions Linked with AND & OR Statements Strings Contained Within SINGLE QUOTES!!

AND & OR Multiple WHERE conditions are Linked by AND / OR Statements AND Means All Conditions are TRUE for the Record OR Means at least 1 of the Conditions is TRUE You May Group Statements with ( ) BE CAREFUL MIXING AND & OR Conditions

Examples with WHERE SELECT * FROM annual_summaries WHERE sd_duration_code = 1 SELECT state_name FROM states WHERE state_population > 15000000

More Examples SELECT state_name, state_population FROM states WHERE state_name LIKE %NORTH% SELECT * FROM annual_summaries WHERE sd_duration_code IN ( 1,( W, X ) AND annual_summary_year = 2000

Be Careful! SELECT mo_mo_id, sd_duration_code FROM annual_summaries WHERE annual_summary_year = 2003 AND values_gt_pri_std > 0 OR values_gt_sec_std > 0 SELECT mo_mo_id, sd_duration_code FROM annual_summaries WHERE annual_summary_year = 2003 AND (values_gt_pri_std > 0 OR values_gt_sec_std > 0)

ORDER BY Statement Defines How the Records are to be Sorted Must be in the SELECT statement to be ORDER BY Default is to order in ASC (Ascending) order Can Sort in Reverse (Descending) Order with DESC After the Column Name

ORDER BY Example SELECT * FROM agencies ORDER BY agency_desc SELECT cc_cn_stt_state_code, site_id FROM sites WHERE lut_land_use_type = MOBILE ORDER BY cc_cn_stt_state_code DESC

Group Functions Performs Common Mathematical Operations on a Group of Records Must define what Constitutes a Group by Using the GROUP BY Clause All non-group elements in the SELECT Statement Must be in the GROUP BY Clause (Additional Columns are Optional)

Group By Example SELECT si_si_id, COUNT(mo_id) FROM monitors GROUP BY si_si_id SELECT AVG(max_sample_value) FROM summary_maximums WHERE max_level <= 3 AND max_ind = REG GROUP BY ans_ans_id

OK, I understand How to Get Data From 1 Table What about Multiple Tables? PARAMETERS PARAMETER_CODE PARAMETER_DESC MONITORS MO_ID SI_SI_ID PA_PARAMETER_CODE POC V_MONITOR_ID MO_ID AIRS_MONITOR_ID STATE_CODE COUNTY_CODE SITE_ID PARAMETER_CODE POC

Primary & Foreign Keys Primary Keys 1 or More Columns Used to Uniquely Identify a record. All Columns Defined as PK s s MUST be populated Foreign Keys Value on a table that references a Primary Key from a different table

Primary & Foreign Keys SITES SI_ID% SITE_LATITUDE SITE_LONGITUDE STREET_ADDRESS PARAMETERS PARAMETER_CODE% PARAMETER_DESC MONITORS MO_ID% SI_SI_ID* PA_PARAMETER_CODE* POC V_MONITOR_ID MO_ID STATE_CODE COUNTY_CODE SITE_ID PARAMETER_CODE POC * = Foreign Key % = Primary Key

Joining Tables PARAMETERS MONITORS MO_ID SI_SI_ID PA_PARAMETER_CODE POC Parameter_Code Parameter_Desc 1 1 44201 1 44201 Ozone 2 1 42101 1 42101 CO 3 4 1 2 42101 81102 2 1 42401 SO2 5 2 44201 1 81102 PM10 6 3 42401 1 Default behavior is to show every possible combination between the two tables

Cartesian Join / Simple Join SELECT mo_id, poc, parameter_desc FROM monitors, parameters PARAMETERS MONITORS Parameter_Code Parameter_Desc MO_ID SI_SI_ID PA_PARAMETER_CODE POC 1 1 44201 1 44201 Ozone 42101 CO 42401 SO2 81102 PM10 2 1 42101 1 3 1 42101 2 4 2 81102 1 5 2 44201 1 6 3 42401 1

Joining Tables SELECT mo_id, poc, parameter_desc FROM monitors, parameters WHERE pa_parameter_code = parameter_code PARAMETERS MONITORS Parameter_Code Parameter_Desc MO_ID SI_SI_ID PA_PARAMETER_CODE POC 1 1 44201 1 44201 Ozone 42101 CO 42401 SO2 81102 PM10 2 1 42101 1 3 1 42101 2 4 2 81102 1 5 2 44201 1 6 3 42401 1

Joining Tables Joins Between Tables are Usually Based on Primary / Foreign Keys Make Sure Joins Between All Tables in the FROM Clause Exist List Joins Between Tables Before Other Selection Elements

Aliases Shorthand for Table or Column References SELECT Aliases Appear as Column Headers in the Output Aliases Cannot be Keywords

Previous SQL With Aliases SELECT mo.mo_id, mo.poc, pa.parameter_desc parameter FROM monitors mo, parameters pa WHERE mo.pa_parameter_code = pa.parameter_code

Why Use an Alias? Saves Typing Good Internal Documentation Better Headers If the same column name exists on multiple tables, SQL needs a way to know which element you are referencing (MO_MO_ID for example)

Recap Basic Structural Elements SELECT FROM WHERE ORDER BY GROUP BY Selecting From Multiple Tables Join Multiple Tables via Primary & Foreign Keys Aliases