Download as pdf or txt
Download as pdf or txt
You are on page 1of 54

Basic SQL

: mySQL for Beginner

Yuttana J.
School of Informatics
tinybronko@gmail.com
1

Todays Agenda
1

Querying data from MySQL database

Modifying data in MySQL

Working with MySQL databases

MySQL Globalization
2

Download MySQL Sample Database

Create new account

10

11

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

SQL statement (REVOKE)


REVOKE ALL PRIVILEGES ON *.* FROM
'user1'@'%'; GRANT SELECT ON *.* TO
'user1'@'%' ;

REVOKE ALL PRIVILEGES ON `user1`.*


FROM 'user1'@'%'; GRANT ALL PRIVILEGES
ON `user1`.* TO 'user1'@'%' WITH GRANT
OPTION;
13

SELECT Statement

14

the syntax of the SELECT statement


SELECT
column_1, column_2, ...
FROM
table_1
WHERE
conditions
ORDER BY column_1 ;
15

16

3 fields lastName, firstName


jobTitle

17

SELECT Statement
SELECT
lastname, firstname, jobtitle
FROM
employees;

18

SELECT Statement

19

Introduction to ORDER BY clause


SELECT column1, column2,...

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

20

Example of ORDER BY clause


SELECT
contactLastname, contactFirstname
FROM
customers
ORDER BY contactLastname;

21

Example of ORDER BY clause


SELECT
contactLastname, contactFirstname
FROM
customers
ORDER BY contactLastname DESC;

22

Example of ORDER BY clause


SELECT
ordernumber, orderlinenumber,
FORMAT(quantityOrdered * priceEach, 2)
FROM
orderdetails
ORDER BY ordernumber , orderLineNumber,
quantityOrdered * priceEach;

23

Example of ORDER BY clause


SELECT
orderNumber, status
FROM
orders
ORDER BY FIELD (status, 'In Process', 'On Hold',
'Cancelled', 'Resolved', 'Disputed', 'Shipped');

24

Using DISTINCT to Eliminate Duplicates

25

syntax of using the DISTINCT clause


SELECT DISTINCT
columns
FROM
table_name
WHERE
where_conditions;
26

DISTINCT example 1
SELECT
lastname
FROM
employees
ORDER BY lastname;

27

DISTINCT example 2
SELECT DISTINCT
lastname
FROM
employees
ORDER BY lastname;

28

DISTINCT with multiple columns


SELECT DISTINCT
state, city
FROM
customers
WHERE
state IS NOT NULL
ORDER BY state , city;
29

DISTINCT with multiple columns


SELECT DISTINCT
state, city
FROM
customers
WHERE
state IS NOT NULL
ORDER BY state , city;
30

DISTINCT and aggregate function


SELECT
COUNT(DISTINCT state)
FROM
customers
WHERE
country = 'USA';
31

Introduction to LIMIT clause


the LIMIT clause syntax with two arguments

SELECT
column1,column2,...
FROM
table
LIMIT offset , count;
32

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

Using LIMIT to get the highest values


SELECT
customernumber, customername, creditlimit
FROM
customers
ORDER BY creditlimit DESC
LIMIT 5;

34

Using LIMIT to get the lowest values


SELECT
customernumber, customername, creditlimit
FROM
customers
ORDER BY creditlimit ASC
LIMIT 5;

35

Using LIMIT to get the lowest values


SELECT
customernumber, customername, creditlimit
FROM
customers
ORDER BY creditlimit ASC
LIMIT 5;

36

BETWEEN Operator Explained

37

Introduction to BETWEEN Operator


The BETWEEN operator allows you to specify a range to
test. We often use the BETWEEN operator in the WHERE
clause of the SELECT, INSERT, UPDATE, and DELETE
statements.
syntax of the BETWEEN operator:

expr [NOT] BETWEEN begin_expr AND end_expr;

38

BETWEEN with number examples


SELECT
productCode, productName, buyPrice
FROM
products
WHERE
buyPrice BETWEEN 90 AND 100;

39

BETWEEN with number examples

40

BETWEEN with number examples 2


SELECT
productCode, productName, buyPrice
FROM
products
WHERE
buyPrice >= 90 AND buyPrice <= 100;

41

BETWEEN with number examples 3


SELECT
productCode, productName, buyPrice
FROM
products
WHERE
buyPrice NOT BETWEEN 20 AND 100;

42

BETWEEN with number examples 4


SELECT
productCode, productName, buyPrice
FROM
products
WHERE
buyPrice < 20 OR buyPrice > 100;

43

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

LIKE with percentage (%) wildcard


SELECT
employeeNumber, lastName, firstName
FROM
employees
WHERE
firstName LIKE 'a%';

45

LIKE with percentage (%) wildcard 2


SELECT
employeeNumber, lastName, firstName
FROM
employees
WHERE
lastName LIKE '%on';

46

LIKE with percentage (%) wildcard 3


SELECT
employeeNumber, lastName, firstName
FROM
employees
WHERE
lastName LIKE '%on%';

47

LIKE with underscore( _ ) wildcard


SELECT
employeeNumber, lastName, firstName
FROM
employees
WHERE
firstName LIKE 'T_m';

48

LIKE operator with NOT operator


SELECT
employeeNumber, lastName, firstName
FROM
employees
WHERE
lastName NOT LIKE 'B%';

49

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

Simple GROUP BY example 1


SELECT
status
FROM
orders
GROUP BY status;

51

GROUP BY with aggregate functions


SELECT
status, COUNT(*)
FROM
orders
GROUP BY status;

52

Simple GROUP BY example 2


SELECT
status, COUNT(*)
FROM
orders
GROUP BY status DESC;

53

54

You might also like