introduction to sql · sql is used in a lot of places big database servers: sql server, mysql,...
TRANSCRIPT
![Page 1: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/1.jpg)
Introduction to SQLBen Smith
Washington State University
![Page 2: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/2.jpg)
SQL is used in a lot of places
Big database servers:
SQL Server, MySQL, Oracle, DB2
But programs can also connect to those servers:
SAS, Python, R
![Page 3: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/3.jpg)
Four Examples
SQLite in Firefox
MySQL & SQLite in R (Omitted)
MS SQL (using ODBC) in SAS
MS SQL Server Manager Studio
![Page 4: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/4.jpg)
Let’s talk about data types
![Page 5: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/5.jpg)
Data types
Chars, Varchars, Text (strings)
Ints, Floats (binary numbers)
Decimal (base 10 number)
![Page 6: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/6.jpg)
I’m proposing there is in fact only one datatype
![Page 7: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/7.jpg)
This is what data really looks like
0011 0 1010010001000100
Op Code
Redirect Bit
Memory Address
http://goo.gl/9nZ9C
![Page 8: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/8.jpg)
Memory really looks like this
0 0 0 0 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 1 1 1 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 1 1 1 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 0 0 1 0 1 0 1 1 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 1 1 1 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 1 1 1 0 0 0 1 0 0 0 0 0 0 1 1
![Page 9: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/9.jpg)
Data types, really
If we group 8 bits together we can represent 255 different things, let’s say we map those to characters of the alphabet
Using this method a bunch of “bytes” (8 bit groups), make up a “string”
Floats (binary decimal numbers) can be represented by taking 1 bit to represent the sign, some number of bits to represent the exponent (e.g. 8) and the rest to represent the fraction (e.g. 23)
![Page 10: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/10.jpg)
Decimal Data TypeJust like 1/3 can’t be perfectly defined in base 10, there are numbers in base 2 that can’t be perfectly defined
The decimal data type solves this issue by storing each individual digit in multiple bits
About 100 times slower than float
![Page 11: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/11.jpg)
So what is a query
It’s a question with three parts:
What do I want
Where is it located
Under what conditions
![Page 12: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/12.jpg)
SyntaxSELECT column, ...
FROM table
... JOIN table ON ...
WHERE column = VALUE
OR column LIKE ‘VALUE’
AND column >= VALUE OR column IN(...)
![Page 13: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/13.jpg)
Think about Joins as Merges
That is you are executing on each table independently then merging the results
![Page 14: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/14.jpg)
Demo
![Page 15: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/15.jpg)
Considering Complicate Conditions
Conditions can be embedded, just like math
![Page 16: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/16.jpg)
ExampleWHERE
(
(student.degree_program_1_major_code IN (@majorone,@majortwo, @majorthree, @majorfour) AND student.[degree_program_1_level_code]=@levelcode AND student.degree_program_1_obj_start_date>bCensus.date AND student.degree_program_1_obj_start_date<=aCensus.date AND student.[center_1_code]=@center_code)
OR
(student.degree_program_2_major_code IN (@majorone,@majortwo, @majorthree, @majorfour) AND student.[degree_program_2_level_code]=@levelcode AND student.degree_program_2_obj_start_date>bCensus.date AND student.degree_program_2_obj_start_date<=aCensus.date AND student.[center_2_code]=@center_code)
)
AND student.enrollment_status_code=3 AND student.total_credits>0 AND student.term_code=CAST((CAST((@myyear+1) AS char(4))+'3') AS INT) AND student.class_standing_code=6
![Page 17: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/17.jpg)
Demo
![Page 18: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/18.jpg)
Groups
So you are always getting a set of results THEN grouping them
Aggregation functions work WITH the group
![Page 19: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/19.jpg)
Example
SELECT COUNT(DISTINCT emplid) AS c,
acad_prog, sex
...
GROUP BY acad_prog, sex
![Page 20: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/20.jpg)
Example
SELECT COUNT(DISTINCT emplid) AS c,
acad_prog, sex, MAX(term_gpa)
...
GROUP BY acad_prog, sex
![Page 21: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/21.jpg)
Demo
![Page 22: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/22.jpg)
Subquery
It is a query inside of a query
It can exist anywhere in the query
Can be slow method, if an approach with Joins is available, do that
![Page 23: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/23.jpg)
ExampleSELECT emplid, (
SELECT TOP 1 gpa
FROM student WHERE s.emplid=emplid
ORDER BY gpa DESC
)
FROM student AS s
![Page 24: Introduction to SQL · SQL is used in a lot of places Big database servers: SQL Server, MySQL, Oracle, DB2 But programs can also connect to those servers: SAS, Python, R. Four Examples](https://reader034.vdocuments.mx/reader034/viewer/2022042713/5fac323d3eed91144b4a1590/html5/thumbnails/24.jpg)
Demo