Más contenido relacionado



Getting Started with SQL Language.pptx

  1. Getting started with the SQL Language Cecilia Brusatori Magna Data Inc
  2. 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
  3. 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
  4. Databases Concepts & Terminology
  5. 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 …
  6. 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
  7. Introduction to SQL
  8. 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:  Declarative Language vs Procedural/imperative language  SQL Standard defined by ANSI (American National Standards Institute)  Vendors variations and extensions.
  9. 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
  10. Language Classifications SQL DDL Create, Alter, Drop, Rename DML Select, Insert, Update, Delete DCL Grant, Revoke, Deny RDBMS Server
  12. 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
  13. 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’, ’’); • DELETE FROM Customer WHERE CustomerID = 123;
  14. Interaction Example SQL Statement SELECT * FROM [Orders] User/App Microsoft SQL Server 1 2 3 4
  15. Let’s Explore SELECT DML
  16. SELECT  Selects Data and Returns a Result-set. (result table)  * Brings all fields  Specify Columns SELECT * FROM Table1; SELECT Column1, Column2, …, ColumnN FROM Table1;
  17. ORDER BY  Sorts the Result_set by the Column(s) specified.  ASC, DESC SELECT Column1, Column2 FROM Table1 ORDER BY Column1 DESC;
  18. 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
  19. 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
  20. 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
  21. GROUP BY Create Subgroups of Tuples SELECT Column1, SUM(Column2) FROM Table1 GROUP BY Column1;
  22. 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;
  23. Syntax Evaluation Order (Ms SQL Server) 1 2 3 4 5 6
  24. Types of JOINS Natural Join, INNER Join, Join Left Outer Join Right Outer Join Full Outer Join
  25. Inner Join SELECT * FROM Table1 T1 INNER JOIN Table2 T2 ON (T1.ID=T2.ID)
  26. Left Outer Join SELECT * FROM Table1 T1 LEFT JOIN Table2 T2 ON (T1.ID=T2.ID)
  27. Right Outer Join SELECT * FROM Table1 T1 RIGHT JOIN Table2 T2 ON (T1.ID=T2.ID)
  28. Full Outer Join SELECT * FROM Table1 T1 FULL OUTER JOIN Table2 T2 ON (T1.ID=T2.ID)
  29. Oracle SQL
  30. Resources  Practice website:  Oracle: database/19/sqlrf/Introduction-to-Oracle-SQL.html  PostgreSQL:  Ms SQL Server: transact-sql/  MySQL:  IBM DB2:
  31. THANK YOU! Connect to me at: /CeBrusa @CeBrusa @MagnaDatainc @MagnaDatainc /company/magnadatainc Presentation:

Hinweis der Redaktion

  1. 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
  2. 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).
  3. 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.
  4. 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.
  5. 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
  6. DEMO Show *, columns selection, alias, Order by
  7. Show some filtering options such as LIKE , =, BETWEEN, AND, OR, NOT, IN, ANY, SOME Precedence
  8. 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;
  9. 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
  10. Most popular functions They are used in the select and having Maybe mention DISTINCT? NULL affects AVG
  11. 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.
  12. Most Common JOINS Allow us to combine tables together