basic sql tutorial

54
Basic SQL โดย Yuttana J. : mySQL for Beginner School of Informatics [email protected] 1

Upload: pongkot-sae-li

Post on 07-Jul-2016

237 views

Category:

Documents


0 download

DESCRIPTION

Basic SQL Tutorial

TRANSCRIPT

Page 1: Basic SQL Tutorial

Basic SQL

โดย Yuttana J.

: mySQL for Beginner

School of [email protected]

1

Page 2: Basic SQL Tutorial

Today’s Agenda

2

Querying data from MySQL database1

Modifying data in MySQL2

Working with MySQL databases3

MySQL Globalization4

Page 3: Basic SQL Tutorial

Download MySQL Sample Database

3

Page 4: Basic SQL Tutorial

4

Page 5: Basic SQL Tutorial

5

Page 6: Basic SQL Tutorial

6

Page 7: Basic SQL Tutorial

7

Page 8: Basic SQL Tutorial

8

Page 9: Basic SQL Tutorial

Create new account

9

Page 10: Basic SQL Tutorial

10

Page 11: Basic SQL Tutorial

11

Page 12: Basic SQL Tutorial

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

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

SELECT Statement

14

Page 15: Basic SQL Tutorial

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

16

Page 17: Basic SQL Tutorial

17

ตอ้งการขอ้มูลจาก 3 fields คือ lastName, firstName และ jobTitle

Page 18: Basic SQL Tutorial

SELECT Statement

SELECT

lastname, firstname, jobtitle

FROM

employees;

18

Page 19: Basic SQL Tutorial

SELECT Statement

19

Page 20: Basic SQL Tutorial

Introduction to ORDER BY clause

SELECT column1, column2,...

FROM tbl

ORDER BY column1 [ASC|DESC], column2 [ASC|DESC],...

20

Page 21: Basic SQL Tutorial

Example of ORDER BY clause

SELECT

contactLastname, contactFirstname

FROM

customers

ORDER BY contactLastname;

21

Page 22: Basic SQL Tutorial

Example of ORDER BY clause

SELECT

contactLastname, contactFirstname

FROM

customers

ORDER BY contactLastname DESC;

22

Page 23: Basic SQL Tutorial

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

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

Using DISTINCT to Eliminate Duplicates

25

Page 26: Basic SQL Tutorial

syntax of using the DISTINCT clause

SELECT DISTINCT

columns

FROM

table_name

WHERE

where_conditions;

26

Page 27: Basic SQL Tutorial

DISTINCT example 1

SELECT

lastname

FROM

employees

ORDER BY lastname;

27

Page 28: Basic SQL Tutorial

DISTINCT example 2

SELECT DISTINCT

lastname

FROM

employees

ORDER BY lastname;

28

Page 29: Basic SQL Tutorial

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

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

DISTINCT and aggregate function

SELECT

COUNT(DISTINCT state)

FROM

customers

WHERE

country = 'USA';

31

Page 32: Basic SQL Tutorial

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

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

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

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

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

BETWEEN Operator Explained

37

Page 38: Basic SQL Tutorial

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

BETWEEN with number examples

SELECT

productCode, productName, buyPrice

FROM

products

WHERE

buyPrice BETWEEN 90 AND 100;

39

Page 40: Basic SQL Tutorial

BETWEEN with number examples

40

Page 41: Basic SQL Tutorial

BETWEEN with number examples 2

SELECT

productCode, productName, buyPrice

FROM

products

WHERE

buyPrice >= 90 AND buyPrice <= 100;

41

Page 42: Basic SQL Tutorial

BETWEEN with number examples 3

SELECT

productCode, productName, buyPrice

FROM

products

WHERE

buyPrice NOT BETWEEN 20 AND 100;

42

Page 43: Basic SQL Tutorial

BETWEEN with number examples 4

SELECT

productCode, productName, buyPrice

FROM

products

WHERE

buyPrice < 20 OR buyPrice > 100;

43

Page 44: Basic SQL Tutorial

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

LIKE with percentage (%) wildcard

SELECT

employeeNumber, lastName, firstName

FROM

employees

WHERE

firstName LIKE 'a%';

45

Page 46: Basic SQL Tutorial

LIKE with percentage (%) wildcard 2

SELECT

employeeNumber, lastName, firstName

FROM

employees

WHERE

lastName LIKE '%on';

46

Page 47: Basic SQL Tutorial

LIKE with percentage (%) wildcard 3

SELECT

employeeNumber, lastName, firstName

FROM

employees

WHERE

lastName LIKE '%on%';

47

Page 48: Basic SQL Tutorial

LIKE with underscore( _ ) wildcard

SELECT

employeeNumber, lastName, firstName

FROM

employees

WHERE

firstName LIKE 'T_m';

48

Page 49: Basic SQL Tutorial

LIKE operator with NOT operator

SELECT

employeeNumber, lastName, firstName

FROM

employees

WHERE

lastName NOT LIKE 'B%';

49

Page 50: Basic SQL Tutorial

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

Simple GROUP BY example 1

SELECT

status

FROM

orders

GROUP BY status;

51

Page 52: Basic SQL Tutorial

GROUP BY with aggregate functions

SELECT

status, COUNT(*)

FROM

orders

GROUP BY status;

52

Page 53: Basic SQL Tutorial

Simple GROUP BY example 2

SELECT

status, COUNT(*)

FROM

orders

GROUP BY status DESC;

53

Page 54: Basic SQL Tutorial

54