There’s a particular style to it…

Slides:



Advertisements
Similar presentations
Basic SQL Introduction Presented by: Madhuri Bhogadi.
Advertisements

Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi Information Systems Spring 2011.
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
Introduction to SQL J.-S. Chou Assistant Professor.
Basic Commands Prepared By: RAHUL PATEL. Create Table Command CREATE TABLE CUSTOMER ( CustomerId int IDENTITY(1,1) PRIMARY KEY, CustomerNumber int NOT.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
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
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Information Technologies and Microsoft SQL Server Day 2 by Alper Özpınar
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Relational Schema and SQL Queries James Wang.
Thinking in Sets and SQL Query Logical Processing.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
There’s a particular style to it… Rob Hatton
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3),
Fundamentals of DBMS Notes-1.
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL Query Getting to the data ……..
Structured Query Language
Chapter 5 Introduction to SQL.
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Top 50 SQL Interview Questions & Answers
Database Systems: Design, Implementation, and Management Tenth Edition
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Lecture#7: Fun with SQL (Part 2)
Using SQL to Prepare Data for Analysis
Structured Query Language (SQL) William Klingelsmith
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Structured Query Language
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL LANGUAGE and Relational Data Model TUTORIAL
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Introduction To Structured Query Language (SQL)
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Database systems Lecture 3 – SQL + CRUD
Chapter 7 Introduction to Structured Query Language (SQL)
SQL Fundamentals in Three Hours
There’s a particular style to it…
C1. SQL BAsic.
Exploring without light….
Introduction To Structured Query Language (SQL)
Contents Preface I Introduction Lesson Objectives I-2
CSC 453 Database Systems Lecture
Database Systems: Design, Implementation, and Management Tenth Edition
There’s a particular style to it…
There’s a particular style to it…
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

There’s a particular style to it… SQL Style There’s a particular style to it… Rob Hatton Email: rob@convitali.com

SQL is not a complete language No way to build a user interface No way to read or write to a file Can’t print Can’t control how things happen It’s awesome at retrieving data!

SQL Server has 2 languages ANSI SQL Declarative TSQL Procedural We’ll be talking about ANSI SQL Focus on sets is the most important concept of the day!

SQL is a different kind of language Most languages are procedural Like telling a story It’s the reason for TSQL SQL is a declarative language Like painting a picture Based on Sets It’s conceptually intertwined with Relational Database structure. Sets are key to understanding SQL

Sets They’re just collections of values: Database Examples: Customers that owe you money Students & grades Cities and temperatures Database Examples: Tables Views Sub-queries Function return Common Table Expressions (sort of)

Types Of SQL Statements Data Definition Language (DDL) Create (Table, View, Proc) Alter (Table, View, Proc) Drop (Table, View, Proc) Data Manipulation Language (DML) Select Insert Delete Update Database definition is stored in a database. DDL manipulates the definition. We’re not going to focus on DDL.

Select Syntax SELECT field_list [ FROM set_source ] (includes joins) [ WHERE search_expression ] (record based) [ GROUP BY group_expression ] [ HAVING aggregate_expression ] [ ORDER BY order_expression [ ASC | DESC ] ] Where clause logic is record based, not set based! ‘Where eyeColor = ‘blue’ and eyeColor = ‘green’ returns no records. ‘Group by’ requires an aggregate function in select list. ‘Having’ only works with group by. Where clause logic is record based, not set based! ‘Where eyeColor = ‘blue’ and eyeColor = ‘green’ returns no records. ‘Group by’ requires an aggregate function in select list. ‘Having’ only works with group by.

Functions and Expressions UPPER() - Converts a field to upper case SUBSTR() - Returns part of a character expression LTRIM() – Remove spaces from left end of text field LEN() - Returns the length of a text field GETDATE() - Returns the system date and time CHARINDEX() – Returns start of string in another string Case When Beware of misuse

Comparison Operators = < <= > <> ! (not) Between Exists In Is [not] null Remember, they are record based not set based!

The importance of NULL NULL is missing data NULL is not the same as zero Needed to calculate valid aggregates Example: When calculating average dress size, it keeps the guys from spoiling the average Anything combined with a NULL is NULL Any relational operator using a NULL returns NULL The ISNULL() function helps deal with NULLs

Aliasing select fis.orderDateKey , max(fis.salesAmount) as maxAmt from factInternetSales fis group by fis.orderDateKey Table Alias: fis Field Alias: maxAmt

Stuff you’ll want * Entity Relationship Diagram (ERD) Picture of database structure Shows Primary/Foreign Keys Management studio Object Explorer Intellisence Drag and drop field names *

It’s important to understand data! Query system tables Profile data --Profile declare @sqlStmt varchar(700) declare @col varchar(50) declare @tbl varchar(50) declare @val varchar(100) set @fldSample = '' set @val = '' declare @fldSample varchar(200) DROP TABLE [dbo].[dbProfile] IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[dbProfile]') AND type in (N'U')) --Recreate the profile table CREATE TABLE [dbo].[dbProfile]( [recordCnt] [int] NULL, [ColumnName] [varchar](50) NOT NULL, [TableName] [varchar](50) NOT NULL, [distinctCnt] [int] NULL, [fldSample] varchar(300) [NullCnt] [int] NULL, [nonNullCnt] [int] NULL, ) ON [PRIMARY] SELECT t.name AS table_name, declare dbSchema cursor for c.name AS column_name ORDER BY table_name INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID FROM sys.tables AS t while @@FETCH_STATUS = 0 fetch next from dbSchema into @tbl, @col open dbSchema begin EXEC (@sqlStmt) set @sqlStmt = 'declare fldList cursor for select distinct ' + @col + ' from ' + @tbl open fldList fetch next from fldList into @val while @@FETCH_STATUS = 0 and len(@fldSample) < 200 set @fldSample = @fldSample + rtrim(ltrim(@val)) + ', ' end deallocate fldList close fldList set @sqlStmt = 'insert into dbProfile (TableName, ColumnName, recordCnt, distinctCnt, nonNullCnt, nullCnt, fldSample) ' set @sqlStmt = @sqlStmt +'count(distinct ['+@col+']) as distinctCnt, ' set @sqlStmt = @sqlStmt +'count(*) as recordCnt, ' set @sqlStmt = @sqlStmt +'select '''+@tbl+''' as TableName ,'''+@col+''' as ColumnName, ' set @sqlStmt = @sqlStmt + '''' + left(@fldSample,200) + '''' set @sqlStmt = @sqlStmt +'sum(case len(rtrim(['+@col+'])) when 0 then 1 else 0 end) as NullCnt, ' set @sqlStmt = @sqlStmt +'sum(case len(rtrim(['+@col+'])) when 0 then 0 else 1 end) as nonNullCnt, ' set @sqlStmt = @sqlStmt +' from ' + @tbl begin try --select @sqlStmt end try end catch print(@sqlStmt) begin catch deallocate dbSchema close dbSchema select * from dbProfile *

Joins Based on values in one table (usually a primary key) matching values in another table (usually a foreign key) Types Inner Outer Left Right Cross Full Self Exists Joins work with any comparison operator! Joins aren’t as much of a problem as data size! --Example Queries go use Fruit select * from fruit select * from colors from fruit select * --Use 'where' clause where colorKey = 1 --Dealing with null where colorKey is null --where colorKey = null --Wildcard --where fruitName like '%e ' where fruitName like '%e%' --where rtrim(fruitName) like '%e' --'In' clause where fruitName in ('apple','grape') --In with a subquery where fruitName in (select fruitName from fruit where rtrim(fruitName) like '%e') --Everything from inner join from fruit f --Relevent columns from inner join on f.colorKey = c.colorKey inner join colors c select f.fruitName, c.color --Everything from left outer join left outer join colors c --Everything from right outer join right outer join colors c --Everything from Cross join (match everything with everything - no 'on' condition) --on f.colorKey = c.colorKey cross join colors c --Everything from full join (combination of left & right outer joins) full join colors c select fruitName --Check for Exists (select * where exists from colors c --Alternate version of Exists where c.colorKey = f.colorKey) where f.colorKey is not null on c.colorKey = f.colorKey select colorKey, count(*) as cnt --Aggregations having count(*) > 1 group by colorKey use [AdventureWorksDW] --Self join from DimAccount c select c.AccountDescription, p.AccountDescription order by c.accountKey on p.ParentAccountKey = c.AccountKey left outer join DimAccount p *

Demo Database Fruit Database Colors table Fruit table Red Yellow Green Orange Fruit table Banana Grape Apple Eaten

Common Table Expressions CTE Create a set on the fly Can only be used in a single query Doesn’t actually create the set Think of it like an include Optimizer doesn’t see a separate set --Common Table Expressions with northAmerica as (select [SalesTerritoryKey], [SalesTerritoryRegion] from DimSalesTerritory where [SalesTerritoryGroup] = 'North America') select top 3 na.SalesTerritoryRegion, fis.SalesAmount from FactInternetSales fis inner join northAmerica na on na.SalesTerritoryKey = fis.SalesTerritoryKey *

Union * Combine 2 sets with matching fields Useful in replacing Case Field count must match exactly Data types must be implicitly convertible First query dictates names Useful in replacing Case Implicitly removes duplicates Use ‘Union All’ to preserve duplicates --Union use [AdventureWorksDW2012] Go select distinct st.SalesTerritoryRegion from FactInternetSales fis inner join DimSalesTerritory st on fis.SalesTerritoryKey = st.SalesTerritoryKey union from FactResellerSales frs on frs.SalesTerritoryKey = st.SalesTerritoryKey *

There’s a particular style to it… SQL Style There’s a particular style to it… Rob Hatton Email: rob@convitali.com