5. WHAT IS SQL?
Structured Query Language
• universal language for creating, modifying
and managing data in relational databases
• 70s - end user tool
• not a programming language!
• But there is the possibility of procedural
extensions
10. MICROSOFT ACCESS И SQL
• Support for two SQL standards:
• ANSI
• MS Jet SQL
• Differences:
• different sets of reserved words and data types
• different rules for the operator Between ... And ...
• different wildcards of the operator Like (? and _, * and %)
• Jet SQL - grouping and sorting by expressions is allowed
• Jet SQL - more complex expressions and additional
instructions
11. "GENERAL" STATEMENTS
ADD COMMIT* FETCH* MAX ROLLBACK*
ALL CONSTRAINT FROM MIN SELECT
ALTER COUNT FOREIGN NOT SET
ANY CREATE GRANT* NULL SOME
ALIAS CREATEVIEW* HAVING ON TRANSACTION*
AS CURRENT* IN OR UNION
ASC CURSOR* INDEX ORDER UNIQUE
AUTHORIZATION* DECLARE* INNER OUTER UPDATE
AVG DELETE INSERT PARAMETERS VALUE
BEGIN* DESC INTO PRIMARY VALUES
BETWEEN DISALLOW IS PRIVILEGES* WHERE
BY DISTINCT JOIN PROCEDURE WORK*
CHECK* DROP KEY REFERENCES
CLOSE* DROPVIEW* LEFT REVOKE*
COLUMN EXISTS LIKE RIGHT
12. OPERATORS
• Most comparison operators match: =, <, <=, >, =>
• The exception is the inequality operator
• != in ANSI SQL
• <> in Jet SQL
13. FUNCTIONS AND OPERATORS
Access ANSI SQL
And AND
Avg( ) AVG()
Between BETWEEN
Count ( ) COUNT
Is IS
Like LIKE
Мах( ) MAX()
Min( ) MIN()
Not NOT
Null NULL
Or OR
Sum( ) SUM
14. NEW IN JET SQL
Access Function Meaning
StdDev()
The offset of the standard deviation for
the sample
StdDevP ( )
The unbiased value of the standard
deviation for the sample
Var () Offset variance value for the sample
VarP ( )
The unbiased variance value for the
sample
15. DATA TYPES
Data types of ANSI SQL Data types of Jet SQL Alias Note
BIT, BIT VARYING BINARY
VARBINARY, BINARY
VARYING BIT VARYING
Not a standard Access data type
UNSUPPORTED BIT
BOOLEAN, LOGICAL,
LOGICAL1, YESNO
Yes/No
UNSUPPORTED TINYINT INTEGER 1, BYTE Byte
UNSUPPORTED COUNTER AUTOINCREMENT
UNSUPPORTED MONEY CURRENCY Currency
DATE, TIME, TIMESTAMP DATETIME DATE, TIME Date/Time
UNSUPPORTED UNIQUEIDEN TIFIER QUID
DECIMAL DECIMAL NUMERIC, DEC
REAL REAL
SINGLE, FLOAT4,
IEEESINGLE
Number (Single)
DOUBLE PRECISION,
FLOAT
FLOAT
DOUBLE, FLOATS,
IEEEDOUBLE, NUMBER
Number (Double)
SMALLINT SMALLINT SHORT, INTEGER2 Number (Integer)
INTEGER INTEGER LONG, INT, INTEGER4 Number (Long Integer)
INTERVAL UNSUPPORTED
UNSUPPORTED IMAGE
LONGBINARY, GENERAL,
OLEOBJECT
OLE field
UNSUPPORTED TEXT
LONGTEXT, LONGCHAR,
MEMO, NOTE, NTEXT
(MEMO) Long Text field
CHARACTER, CHARACTER
VARYING, NATIONAL
CHARACTER, NATIONAL
CHARACTER VARYING
CHAR
TEXT(n), ALPHANUMERIC,
CHARACTER, STRING,
VARCHAR, CHARACTER
VARYING, NCHAR,
NATIONAL CHARACTER,
NATIONAL CHAR,
NATIONAL CHARACTER
VARYING, NATIONAL
CHAR VARYING
Short Text field
16. WILDCARD CHARACTERS
Jet SQL ANSI SQL Note
? _ (underscore) Any single character
* %
Arbitrary number of
characters
# No equivalent Any digit from 0 to 9
[character_list] No equivalent
Any single character
from the character list
[!character_list] No equivalent
Any single character
not included in the
character list
17. SEPARATORS AND CHARACTERS
Commas are used to separate list members
• First Name, Last Name, Middle Name,Year of Birth,Address, City, Index
Square brackets are used to specify field names that contain spaces
• [Date of placement]
If the query includes fields from several tables, then the full field name is
included
• the name of the table and the name of the field, between which the point is
used
• Orders.Order_code
Characters are enclosed in both single (') and double quotation marks (")
• when using SQL statements inVBA procedures, it is recommended
to put single quotes
At the end of the Jet SQL statement, a semicolon (;) is mandatory
18. CREATING SQL QUERIES
Create a query using the DesignView
Close the Show Table dialog box without adding tables
ChooseView > SQLView on the ribbon
Delete all text in SQL window
• usually the default is SELECT DISTINCTROW;
Enter SQL statement
• CTRL+ENTER for a new line
Click the Run (!) button on the ribbon
20. EXAMPLES
• SELECTTOP 25 * FROM Orders IN "С:DocumentsNorthWind.mdb";
• SELECT Ordered.*, Orders.CustomerID, Orders.OrderDate,
Orders.OrderID FROM Orders, Ordered
WHERE (((Orders.OrderDate)>=#1/1/2018#) AND
((Orders.OrderID)=[Ordered].[OrderID])) OR
(((Orders.OrderDate) Is Null) AND
((Orders.OrderID)=[Ordered].[OrderID]));
• SELECT Customers.Title, Customers.City, Customers.Address
FROM Customers
WHERE (((Customers.Country)
IN (‘Ukraine’,’Poland’,’Georgia')));
21. ELIMINATION OF REPETITIONS
• DISTINCTROW and DISTINCT in the SELECT
statement allow you to exclude duplicate lines
from the result set
• DISTINCTROW - all fields of the source table are
used to compare records.
• no matter which fields are included in the query
• DISTINCT - only those fields that are included in
the query are used to compare records.
22. AGGREGATE FUNCTIONS
• AVG () - average
• COUNT () - the number of rows
• FIRST () - first value
• LAST () - the last value
• MAX () - the highest value
• MIN () - the lowest value
• SUM () - amount
23. SCALAR FUNCTIONS
• UCASE () - upper case
• LCASE () - lower case
• MID () - extract characters from text
• LEN () - text length
• ROUND () - rounding value
• NOW () - current date-time
• FORMAT () - formatting values
24. EXAMPLES
• SELECT Count(*) AS [Number of Invoices]
FROM tblInvoices
• SELECT Avg(Amount) AS [Average Invoice
Amount] FROM tblInvoices
• SELECT Last(Amount) AS [Last Invoice Amount]
FROM tblInvoices
25. LESSON 2: SQL QUERIES
• Queries to add, update,
delete, create a table
26. QUERIESTO ADD
INSERT INTO destination [(field_1[, field_2[, ...]])] [IN externalDataBase]
SELECT [source.]field_1[, field_2[, ...]
FROM expression [, ...] [IN externalDataBase]
[WHERE...]
[GROUP BY...]
[HAVING...]
[ORDER BY...]
[WITH OWNERACCESS OPTION]
Examples:
• INSERT INTO Delivery (Title, PhoneNumber)
VALUES (“DHL",“+3(095)211-9988");
• INSERT INTOTempTable ( CustomerID, [OrderAmount] )
SELECT Orders.CustomerID,sum([Price]*[Quantity])
FROM Ordered, Orders IN "С:DocumentsDataBasel.mdb"
WHERE (Ordered.OrderID)=[Orders].[OrderID]))
GROUP BY Orders.CustomerID
ORDER BY Sum([Price]*[Quantity])
DESC;
27. QUERIESTO UPDATE
• UPDATE table SET field=newValue WHERE criteria
• Examples:
• UPDATE [Products] SET [Products].Discontinued = False
WHERE ((([Products].Discontinued)=Тruе));
• UPDATE Orders INNER JOIN Ordered ON
Orders.OrderID= Ordered.OrderID SET
Ordered.Discount = 0 WHERE (((Orders.OrderDate) Is
Null));
28. QUERIESTO DELETE
• DELETE [table.]*, field_1 [,...] FROM table
WHERE criteria
• Example:
• DELETE [Products].Discontinued FROM [Products]
WHERE ((([Products].Discontinued)=Тruе));
• DROP {TABLE table | INDEX index ON table |
PROCEDURE procedure |VIEW view}
30. EXERCISE
1. Try to recreate the same
queries for your database
using SQL
31. BOTTOM LINE
• SQL expressions. Basic language constructs
• SELECT statement. Build simple queries with
selection conditions
• COUNT, FIRST, LAST statistical functions
• Statistical functions MIN, MAX,AVG. SUM
function
• Queries to add, update, delete, create a table
33. SELF-TEST
• What SQL standards are supported by Microsoft
Access?What are the differences between them?
• How to create a SQL query in Access?
• List Access SQL QueryTypes
34. WANT TO KNOW MORE?
W3Schools SQL Tutorial
https://www.w3schools.com/sql/default.asp
35. WANT TO KNOW MORE?
Sololearn SQL Fundamentals
https://www.sololearn.com/Course/SQL/
36. WANT TO KNOW MORE?
SQL Course - QA
https://www.slideshare.net/pingkapil/sql-course-qa