MagnaDataSolutions.com
Cecilia Brusatori
ready
Founder and CEO of Magna Data inc
Data Analytics Solutions and Corporate Training
University Graduate Degree in Computer Science
& Engineering
Tech Community Leader & Organizer in South
Florida US
Proudly Nerd - Always Learning - Naturist –
Love Animals, good Books & the Beach
Entrepreneur - Consultant, Trainer, Speaker
Miami/Fort Lauderdale Area
MagnaDataSolutions.com
Agenda
DBs Concepts & Terminology
Intro to SQL Language
DML Syntax: SELECT, UPDATE, INSERT, DELETE
Exploring SELECT
WHERE, Operators
Aggregations
Group By, Having
Types of Joins
MagnaDataSolutions.com
RDBMS
Relational Database Management Systems
Software that provides Users and
Applications, with processes to: Define,
Create, Manipulate and Share (Relational)
Databases
User Applications
RDBMS Server
…
MagnaDataSolutions.com
Relational Databases
Database > Tables > Rows > Columns
1
1..*
1
1
0..*
1
0..*
0..*
ERD: display entities
and how they are
related
Primary Keys (PK):
Uniquely identifies
each record in a table
Foreign Keys (FK):
refers to the Primary
Key in another table
MagnaDataSolutions.com
About the SQL Language
Structured Query Language
SQL / SEQUEL (Structured English Query Language)
Interact with Relational Databases
Comes from Relational Algebra & Calculus.
Codd, Chamberlin & Boyce.
SQL history by Chamberlin, here: https://ieeexplore.ieee.org/document/6359709
Declarative Language vs Procedural/imperative language
SQL Standard defined by ANSI (American National Standards Institute)
Vendors variations and extensions.
MagnaDataSolutions.com
Extensions to SQL Language
Add Procedural Programming Language Functionality (Functions,
Cursors, arrays, loops, Control of Flows, etc)
Oracle PL/SQL Procedural Language/SQL
Microsoft SQL Server T-SQL Transact-SQL
Informix SPL Stored Procedural Language
MySQL SQL/PSM SQL/Persistent Stored Module
PostgreSQL PL/pgSQL Procedural Language/PostgreSQL SQL
SAP R3 ABAP Advance Business Application Programming
MariaDB SQL/PSM SQL/Persistent Stored Module
MagnaDataSolutions.com
DML - Syntax
SELECT Column1, Column2, … FROM Table1
SELECT Column1, Column2, … FROM Table1 WHERE Condition
INSERT INTO Table1 (Column1,Column2,…) VALUES(Value1,Value2,…)
UPDATE Table1 SET Column1=Value1, Column2=Value2,…
UPDATE Table1 SET Column1=Value1, Column2=Value2,… WHERE Condition
DELETE FROM Table1
DELETE FROM Table1 WHERE Condition
MagnaDataSolutions.com
DML – Syntax - Examples
• SELECT FirstName, LastName, email, DoB, FROM Customer;
• UPDATE Customer SET LastName=‘Smith’ WHERE CustomerID=123;
• INSERT INTO Customer(FirstName, LastName, email)
VALUES (‘Jane’, ’Doe’, ’Jdoe@gmail.com’);
• DELETE FROM Customer WHERE CustomerID = 123;
MagnaDataSolutions.com
SELECT
Selects Data and Returns a Result-set. (result table)
* Brings all fields
Specify Columns
SELECT * FROM Table1;
SELECT Column1, Column2, …, ColumnN FROM Table1;
MagnaDataSolutions.com
WHERE Clause
SELECT Column1, Column2
FROM Table1
WHERE Condition;
Conditionals:
Boolean operators/Logical Operators AND, OR, NOT
Comparison operators:
=,<>,<=,>. Some DBs inequality: !=,^=,<>
ANY/SOME
EXISTS
LIKE
BETWEEN
IS NULL
MagnaDataSolutions.com
NULL values
Represents “Nothingness”. It is not a Value. It is an Empty field
Origin: Value not entered/recorded
May affect results (Example with AVG)
Used with operators IS/NOT: IS NULL, IS NOT NULL
MagnaDataSolutions.com
Aggregations
Function Description
COUNT() Counts elements in a column. COUNT (*) counts tuples.
SUM() Returns the Summarization of all values in a column.
AVG() Returns the Average from values in a column.
MAX() Returns the Maximum value in a column.
MIN() Returns the Minimum value in a column
MagnaDataSolutions.com
HAVING
HAVING clause: contains conditions on aggregates
Specifies a search condition for Groups
SELECT Column1, SUM(Column2)
FROM Table1
GROUP BY Column1
HAVING SUM(Column2) >10000;
MagnaDataSolutions.com
Resources
Practice website:
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
Oracle:
https://docs.oracle.com/en/database/oracle/oracle-
database/19/sqlrf/Introduction-to-Oracle-SQL.html
PostgreSQL:
https://www.postgresql.org/docs/current/sql.html
Ms SQL Server:
https://learn.microsoft.com/en-us/training/paths/get-started-querying-with-
transact-sql/
MySQL:
https://dev.mysql.com/doc/refman/8.0/en/sql-statements.html
IBM DB2:
https://www.ibm.com/docs/en/db2-for-zos/11?topic=db2-sql
MagnaDataSolutions.com
THANK YOU!
Connect to me at: Cecilia@MagnaDataSolutions.com
/CeBrusa
@CeBrusa
@MagnaDatainc
@MagnaDatainc
/company/magnadatainc
https://magnadatainc.com
Presentation:
www.MagnaDataSolutions.com/intro-to-sql-language
Editor's Notes
Relational Databases store data organized i Tables that contains rows and columns
Data is typically structured across multiple tables, which can be joined together via a primary key or a foreign key.
Orders can have 1 or many order lines here, but one OrderLine/Detail belong to only
There is a SQL language standard defined by the American National Standards Institute (ANSI). Each vendor adds their own variations and extensions.
Declarative Language (define the output) vs Procedural/imperative language (sequence of instructions)
Declarative languages express the logic of a computation without describing its control flow in detail. Declarative programming stands in contrast to imperative programming via imperative programming languages, where control flow is specified by serial orders (imperatives).
T-SQL expands on the SQL standard to include procedural programming, local variables, various support functions for string processing, date processing, mathematics, etc. and changes to the DELETE and UPDATE statements.
Stored procedures in SQL Server are executable server-side routines. The advantage of stored procedures is the ability to pass parameters.
Data Definition Language (DDL) is the set of SQL statements that handles the definition of database objects, such as tables, views, and procedures. DDL includes statements such as CREATE, ALTER, and DROP.
Data Manipulation Language (DML) is the set of SQL statements that focuses on querying and modifying data. DML statements include SELECT, INSERT, UPDATE, and DELETE.
Data Control Language (DCL) is the set of SQL statements used to manage security permissions for users and objects. DCL includes statements such as GRANT, REVOKE, and DENY.
Add an example for each and the outcome as a pop up animation
SQL Keywords are case sensitive. Some DB configurations are setup to be case sensitive for Tables and Column names. This will be referred as the Collation. It can be setup at a Database Level, Table level or column level.
SQL Server default collation is case insensitive.
PostgreSQL is Case Sensitive by default
DEMO
Show *, columns selection, alias,
Order by
Show some filtering options such as LIKE , =, BETWEEN, AND, OR, NOT, IN, ANY, SOME
Precedence
Comparing to NULL returns UNKNOWN when using ANSI. SQL Server lets you turn ANSI NULL OFF and use other operators that will return TRUE or FALSE
There are vendors built-in functions to handle nulls
SELECT FirstName, ISNULL(MiddleName, 'None') AS MiddleIfAny, LastName FROM Sales.Customer;
In is more meticulous, goes thru the entire set
Exists, we don’t care how many times, if it finds it, then it is true, otherwise is null
Most popular functions
They are used in the select and having
Maybe mention DISTINCT?
NULL affects AVG
Now that you've seen what each clause does, let's look at the order in which SQL Server actually evaluates them:
The FROM clause is evaluated first, to provide the source rows for the rest of the statement. A virtual table is created and passed to the next step.
The WHERE clause is next to be evaluated, filtering those rows from the source table that match a predicate. The filtered virtual table is passed to the next step.
GROUP BY is next, organizing the rows in the virtual table according to unique values found in the GROUP BY list. A new virtual table is created, containing the list of groups, and is passed to the next step. From this point in the flow of operations, only columns in the GROUP BY list or aggregate functions may be referenced by other elements.
The HAVING clause is evaluated next, filtering out entire groups based on its predicate. The virtual table created in step 3 is filtered and passed to the next step.
The SELECT clause finally executes, determining which columns will appear in the query results. Because the SELECT clause is evaluated after the other steps, any column aliases (in our example, Orders) created there cannot be used in the GROUP BY or HAVING clause.
The ORDER BY clause is the last to execute, sorting the rows as determined by its column list.
https://learn.microsoft.com/en-us/training/modules/introduction-to-transact-sql/4-examine-select-statement
Most Common JOINS
Allow us to combine tables together