0% found this document useful (0 votes)
45 views10 pages

Mysql Notes

The document discusses various SQL operators like BETWEEN, LIKE, REGEXP, IS NULL, ORDER BY and LIMIT that are used to select rows from tables based on certain conditions. It also explains different types of joins like inner joins, self joins and joins across multiple tables from different databases.

Uploaded by

MeerShahrukh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views10 pages

Mysql Notes

The document discusses various SQL operators like BETWEEN, LIKE, REGEXP, IS NULL, ORDER BY and LIMIT that are used to select rows from tables based on certain conditions. It also explains different types of joins like inner joins, self joins and joins across multiple tables from different databases.

Uploaded by

MeerShahrukh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

-- between operator in SQL

Retrive the customers having points between 1000 and 3000

SELECT *

FROM customers

WHERE points BETWEEN 1000 AND 3000

Retrive the customer born between 1/1/1990 and 1/1/2000

SELECT *

FROM customers

WHERE birth_date BETWEEN '1990-01-01' AND '2000-01-01'

-- The LIKE Operator: retrives the string matches specific pattern

-- find the customers whose last name starts with b

SELECT *

FROM customers

WHERE last_name LIKE 'b%'

-- find the customers whose last name contains b at any place

SELECT *

FROM customers

WHERE last_name LIKE '%se%'

-- find the customers whose last name ends with 'ey'

SELECT *

FROM customers
WHERE last_name LIKE '%ey'

-- find the customers whose last name exactly contains two characters and last
character ends with 'y'

SELECT *

FROM customers

WHERE last_name LIKE '_y'

-- find the customers whose last name exactly contains 6 characters and last
character ends with 'y'

SELECT *

FROM customers

WHERE last_name LIKE '_____y'

-- find the customers whose last name exactly contains 6 characters, first
character starts with b and last character is 'y'

SELECT *

FROM customers

WHERE last_name LIKE 'b____y'

-- get the customers whose addresses contains TRIAL or AVENUE

SELECT *

FROM customers

WHERE address LIKE '%TRAIL%'


OR address LIKE '%AVENUE%'

-- get the customers whose phone # ends with 9

SELECT *

FROM customers
WHERE phone LIKE '%9'

-- We can also use NOT LIKE to get the results other than string pattern

-- REGEXP OPERATOR IN SQL

-- get the customers whose last name contains the field

SELECT *

FROM customers

WHERE last_name REGEXP 'field'

-- get the customers whose last name contains the field and mac

SELECT *
FROM customers
WHERE last_name REGEXP 'field|mac'

-- get the customer whose last name contains the field in the start

SELECT *

FROM customers

WHERE last_name REGEXP '^field'

-- get the customer whose last name contains the field in the end

SELECT *

FROM customers

WHERE last_name REGEXP 'field$'

-- get the customer whose last name contains the ie, ge, me any where
SELECT *

FROM customers
WHERE last_name REGEXP '[gim]e'

-- get the customer whose last name contains the ie, ge, me or mg, ag any where

SELECT *

FROM customers

WHERE last_name REGEXP '[gim]e'

OR last_name REGEXP '[ma]g'

-- We can also have a range of character

-- get the customeres whose last name contains the ae, be, ce, de, ee anywhere

SELECT *

FROM customers

WHERE last_name REGEXP '[a-e]e'

-- get the customeres whose first name contains the le, lk

SELECT *

FROM customers

WHERE first_name REGEXP 'l[ek]'

-- get the customeres whose first name are ELKA or AMBUR

SELECT *

FROM customers

WHERE first_name REGEXP 'ELKA|AMBUR'


-- get the customeres whose last name ends with EY or ON

SELECT *

FROM customers

WHERE last_name REGEXP 'EY$|ON$'

-- get the customeres whose last name starts with MY or contains SE

SELECT *

FROM customers

WHERE last_name REGEXP '^MY|SE'

-- get the customeres whose last name contains B followed by R or U

SELECT *

FROM customers

WHERE last_name REGEXP 'B[R|U]'

-- or WHERE last_name REGEXP 'B[RU]'

-- or WHERE last_name REGEXP 'BR|BU'

--- IS NULL Operator


---

-- get the customer whose phone # is null

SELECT *

FROM customers

WHERE phone IS NULL

-- get the customer whose phone # is not null


SELECT *

FROM customers

WHERE phone IS NOT NULL

-- Get the orders that are not shipped

SELECT *

FROM orders

WHERE shipped_date IS NULL


-- or WHERE shipper_id IS NULL

-- The ORDER BY

-- Sorting the data in different orders

-- get the costumers with their first names sorted in ascending order.

SELECT *

FROM customers

ORDER BY first_name

-- get the costumers with their first names sorted in descending order.

SELECT *

FROM customers

ORDER BY first_name DESC

-- get the costumers with their first names sorted in descending order.

SELECT *

FROM customers

ORDER BY state, first_name

-- We can also add a pseudo column giving an alias to it such as points etc and
order by point.
SELECT first_name, last_name, 10 AS points

FROM customers

ORDER BY points, first_name

-- get the data of orders whose order it is 2 and show the data in order of
decreasing total price

SELECT *, quantity*unit_price AS total_price

FROM order_items

WHERE order_id = 2

ORDER BY total_price DESC

-- The LIMIT CLAUSE

-- it is used to limit the data shown to you, if you have 10 rows and u want to see
only 3 rows

SELECT *

FROM customers
LIMIT 3

-- Suppose we have data 9 rows customers and we have divided it into pages

-- page 1: 1 - 3

-- page 2: 4 - 6

-- page 3: 7 - 9

-- we only want to get the data of customers of page 3 we can do following:

SELECT *

FROM customers

LIMIT 6, 3

-- So the above written line will start to print the data after six and will print
the 3 entries.

-- GET top three loyal customers.

-- Ans: loyal means customer having greater points value.

SELECT *

FROM customers

ORDER BY points DESC

lIMIT 3

USE sql_store
-- Inner JOINS: get the order id with customer id and customer's full name

SELECT order_id, customers.customer_id, first_name, last_name

FROM orders

JOIN customers

ON orders.customer_id = custmomers.customer_id

SELECT oi.product_id, p.name, quantity, oi.unit_price

FROM order_items oi

JOIN products p
ON oi.product_id = p.product_id

SELECT order_id, o.customer_id, first_name, last_name


FROM orders o
JOIN customers c
ON o.customer_id = c.customer_id

SE sql_store;
-- Joining accross DATABASES

-- Suppose we do not have products tables in the sql_store, but we have products
table in the sql_inventory

-- and we want to retrive data from order_items along with sql_inventory database
products table and

-- this is joining accross databases so for that we do following...

SELECT *

FROM order_items oi

JOIN sql_inventory.products p

ON oi.product_id = p.product_id

USE sql_inventory;

-- Suppose we use sql_inventory instead of sql_store that we have use above, we


will have following changes...

SELECT oi.order_id , p.name, p.unit_price

FROM sql_store.order_items oi

JOIN products p

ON oi.product_id = p.product_id

So, we will only prefix the tables that are not in the currently used database, if
we see the above example, we have used sql_inventory database and we have prefixed
only the joined data base that is sql_store.

-- SELF JOINS

-- We can also join tables with them selves.

USE sql_hr;

SELECT *

FROM employees e

JOIN employees m

ON e.reports_to = m.employee_id

for the specific columns like employee_id, firstname of employee, and manager

USE sql_hr;
SELECT
e.employee_id,
e.first_name,
m.first_name AS manager
FROM employees e

JOIN employees m

ON e.reports_to = m.employee_id

USE sql_store;

-- JOINING MULTIPLE TABLES

-- Here we are joining three tables, orders, customers, order_statuses we want


results of columns like order_id, order_date,

-- first_name, last_name, and name from the statuses table aliased as status.

SELECT o.order_id, o.order_date, c.first_name, c.last_name, os.name AS status


FROM orders o

JOIN customers c

ON o.order_id = c.customer_id

JOIN order_statuses os

ON o.status = os.order_status_id

this query will provide the desired results.

USE sql_invoicing;

SELECT
p.date, p.invoice_id, p.amount,
c.name,
pm.name
FROM payments p
JOIN clients c
ON p.client_id = c.client_id
JOIN payment_methods pm
ON p.payment_method = pm.payment_method_id

You might also like