basic sql tutorial
DESCRIPTION
Basic SQL TutorialTRANSCRIPT
![Page 2: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/2.jpg)
Today’s Agenda
2
Querying data from MySQL database1
Modifying data in MySQL2
Working with MySQL databases3
MySQL Globalization4
![Page 3: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/3.jpg)
Download MySQL Sample Database
3
![Page 4: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/4.jpg)
4
![Page 5: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/5.jpg)
5
![Page 6: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/6.jpg)
6
![Page 7: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/7.jpg)
7
![Page 8: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/8.jpg)
8
![Page 9: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/9.jpg)
Create new account
9
![Page 10: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/10.jpg)
10
![Page 11: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/11.jpg)
11
![Page 12: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/12.jpg)
SQL statement
CREATE USER 'user1'@'%' IDENTIFIED WITH mysql_native_password AS '***';
GRANT ALL PRIVILEGES ON *.* TO 'user1'@'%' REQUIRE NONE WITH GRANT OPTION;
CREATE DATABASE IF NOT EXISTS `user1`; GRANT ALL PRIVILEGES ON `user1`.* TO 'user1'@'%'; GRANT ALL PRIVILEGES ON `user1\_%`.* TO 'user1'@'%';
12
![Page 13: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/13.jpg)
SQL statement (REVOKE)
REVOKE ALL PRIVILEGES ON *.* FROM'user1'@'%'; GRANT SELECT ON *.* TO'user1'@'%' ;
13
REVOKE ALL PRIVILEGES ON `user1`.* FROM 'user1'@'%'; GRANT ALL PRIVILEGESON `user1`.* TO 'user1'@'%' WITH GRANT OPTION;
![Page 14: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/14.jpg)
SELECT Statement
14
![Page 15: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/15.jpg)
the syntax of the SELECT statement
SELECT
column_1, column_2, ...
FROM
table_1
WHERE
conditions
ORDER BY column_1 ;
15
![Page 16: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/16.jpg)
16
![Page 17: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/17.jpg)
17
ตอ้งการขอ้มูลจาก 3 fields คือ lastName, firstName และ jobTitle
![Page 18: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/18.jpg)
SELECT Statement
SELECT
lastname, firstname, jobtitle
FROM
employees;
18
![Page 19: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/19.jpg)
SELECT Statement
19
![Page 20: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/20.jpg)
Introduction to ORDER BY clause
SELECT column1, column2,...
FROM tbl
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC],...
20
![Page 21: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/21.jpg)
Example of ORDER BY clause
SELECT
contactLastname, contactFirstname
FROM
customers
ORDER BY contactLastname;
21
![Page 22: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/22.jpg)
Example of ORDER BY clause
SELECT
contactLastname, contactFirstname
FROM
customers
ORDER BY contactLastname DESC;
22
![Page 23: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/23.jpg)
Example of ORDER BY clause
SELECT
ordernumber, orderlinenumber, FORMAT(quantityOrdered * priceEach, 2)
FROM
orderdetails
ORDER BY ordernumber , orderLineNumber, quantityOrdered * priceEach;
23
![Page 24: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/24.jpg)
Example of ORDER BY clause
SELECT
orderNumber, status
FROM
orders
ORDER BY FIELD (status, 'In Process', 'On Hold', 'Cancelled', 'Resolved', 'Disputed', 'Shipped');
24
![Page 25: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/25.jpg)
Using DISTINCT to Eliminate Duplicates
25
![Page 26: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/26.jpg)
syntax of using the DISTINCT clause
SELECT DISTINCT
columns
FROM
table_name
WHERE
where_conditions;
26
![Page 27: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/27.jpg)
DISTINCT example 1
SELECT
lastname
FROM
employees
ORDER BY lastname;
27
![Page 28: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/28.jpg)
DISTINCT example 2
SELECT DISTINCT
lastname
FROM
employees
ORDER BY lastname;
28
![Page 29: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/29.jpg)
DISTINCT with multiple columns
SELECT DISTINCT
state, city
FROM
customers
WHERE
state IS NOT NULL
ORDER BY state , city;
29
![Page 30: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/30.jpg)
DISTINCT with multiple columns
SELECT DISTINCT
state, city
FROM
customers
WHERE
state IS NOT NULL
ORDER BY state , city;
30
![Page 31: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/31.jpg)
DISTINCT and aggregate function
SELECT
COUNT(DISTINCT state)
FROM
customers
WHERE
country = 'USA';
31
![Page 32: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/32.jpg)
Introduction to LIMIT clause
the LIMIT clause syntax with two arguments…
SELECT
column1,column2,...
FROM
table
LIMIT offset , count;
32
![Page 33: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/33.jpg)
Using LIMIT to get the first N rows
You can use the LIMIT clause to select the first N rows in a table, for example…
SELECT
customernumber, customername, creditlimit
FROM
customers
LIMIT 10;
33
![Page 34: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/34.jpg)
Using LIMIT to get the highest values
SELECT
customernumber, customername, creditlimit
FROM
customers
ORDER BY creditlimit DESC
LIMIT 5;
34
![Page 35: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/35.jpg)
Using LIMIT to get the lowest values
SELECT
customernumber, customername, creditlimit
FROM
customers
ORDER BY creditlimit ASC
LIMIT 5;
35
![Page 36: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/36.jpg)
Using LIMIT to get the lowest values
SELECT
customernumber, customername, creditlimit
FROM
customers
ORDER BY creditlimit ASC
LIMIT 5;
36
![Page 37: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/37.jpg)
BETWEEN Operator Explained
37
![Page 38: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/38.jpg)
Introduction to BETWEEN Operator
The BETWEEN operator allows you to specify a range to test. We often use the BETWEEN operator in the WHEREclause of the SELECT, INSERT, UPDATE, and DELETEstatements.
38
syntax of the BETWEEN operator:
expr [NOT] BETWEEN begin_expr AND end_expr;
![Page 39: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/39.jpg)
BETWEEN with number examples
SELECT
productCode, productName, buyPrice
FROM
products
WHERE
buyPrice BETWEEN 90 AND 100;
39
![Page 40: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/40.jpg)
BETWEEN with number examples
40
![Page 41: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/41.jpg)
BETWEEN with number examples 2
SELECT
productCode, productName, buyPrice
FROM
products
WHERE
buyPrice >= 90 AND buyPrice <= 100;
41
![Page 42: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/42.jpg)
BETWEEN with number examples 3
SELECT
productCode, productName, buyPrice
FROM
products
WHERE
buyPrice NOT BETWEEN 20 AND 100;
42
![Page 43: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/43.jpg)
BETWEEN with number examples 4
SELECT
productCode, productName, buyPrice
FROM
products
WHERE
buyPrice < 20 OR buyPrice > 100;
43
![Page 44: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/44.jpg)
Using LIKE Operator To Select Data Based
• The percentage ( % ) wildcard allows you to match any string of zero or more characters.
• The underscore ( _ ) wildcard allows you to match any single character.
44
![Page 45: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/45.jpg)
LIKE with percentage (%) wildcard
SELECT
employeeNumber, lastName, firstName
FROM
employees
WHERE
firstName LIKE 'a%';
45
![Page 46: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/46.jpg)
LIKE with percentage (%) wildcard 2
SELECT
employeeNumber, lastName, firstName
FROM
employees
WHERE
lastName LIKE '%on';
46
![Page 47: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/47.jpg)
LIKE with percentage (%) wildcard 3
SELECT
employeeNumber, lastName, firstName
FROM
employees
WHERE
lastName LIKE '%on%';
47
![Page 48: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/48.jpg)
LIKE with underscore( _ ) wildcard
SELECT
employeeNumber, lastName, firstName
FROM
employees
WHERE
firstName LIKE 'T_m';
48
![Page 49: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/49.jpg)
LIKE operator with NOT operator
SELECT
employeeNumber, lastName, firstName
FROM
employees
WHERE
lastName NOT LIKE 'B%';
49
![Page 50: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/50.jpg)
Introducing to GROUP BY clause
The following illustrates the GROUP BY clause syntax:
SELECT c1, c2,..., cn, aggregate_function(ci)
FROM table
WHERE where_conditions
GROUP BY c1 , c2,...,cn;
50
![Page 51: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/51.jpg)
Simple GROUP BY example 1
SELECT
status
FROM
orders
GROUP BY status;
51
![Page 52: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/52.jpg)
GROUP BY with aggregate functions
SELECT
status, COUNT(*)
FROM
orders
GROUP BY status;
52
![Page 53: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/53.jpg)
Simple GROUP BY example 2
SELECT
status, COUNT(*)
FROM
orders
GROUP BY status DESC;
53
![Page 54: Basic SQL Tutorial](https://reader034.vdocuments.pub/reader034/viewer/2022050803/577c778b1a28abe0548c8478/html5/thumbnails/54.jpg)
54