0% found this document useful (0 votes)
3 views5 pages

SQL Code With Mosh

The document provides an overview of SQL SELECT statements, including various clauses such as WHERE, ORDER BY, and JOINs. It explains the use of operators like IN, BETWEEN, LIKE, and REGEXP for filtering data, as well as the concept of NULL values. Additionally, it covers different types of joins, including INNER, OUTER, and SELF JOINs, along with examples for each type.

Uploaded by

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

SQL Code With Mosh

The document provides an overview of SQL SELECT statements, including various clauses such as WHERE, ORDER BY, and JOINs. It explains the use of operators like IN, BETWEEN, LIKE, and REGEXP for filtering data, as well as the concept of NULL values. Additionally, it covers different types of joins, including INNER, OUTER, and SELF JOINs, along with examples for each type.

Uploaded by

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

SQL: Code with Mosh

SELECT Statement
SELECT *
FROM table_name
WHERE condition
ORDER BY asc

Select
name,
‘unit price’,
‘unit price’*1.1 as ‘New Price’
From Product

WHERE CLAUSE
columni
state = ‘MP’
!= or <>
birthdate > ‘2004-07-18’
>=
<
<=

SELECT * FROM orders WHERE orderdate >= ‘2024-01-01’

birthdate > ‘2004-07-18’ AND points >1000


birthdate > ‘2004-07-18’ OR points >1000
birthdate > ‘2004-07-18’ OR points >1000 AND state=‘MP’
()
*, /
+,-

AND
OR
NOT (birthdate > ‘2004-07-18’ OR points >1000 AND state=‘MP’)
==
birthdate <= ‘2004-07-18’ AND (points <= 1000 OR state <> ‘MP’)

IN OPERATOR
Select * FROM customers WHERE id IN (100,200,300)

BETWEEN OPERATOR
Select * FROM customers WHERE points BETWEEN 1000 AND 3000
SELECT * FROM orders WHERE orderdate BETWEEN ‘2024-01-01’ AND
‘2024-12-31’

LIKE OPERATOR
Starts with pat
SELECT * FROM customers WHERE name LIKE ‘pat%’
Has pat in name ‘%pat%
Ends with pat ‘%pat’
Second letter is pat ‘_pat%’
Exactly 6 letter start with pat ‘pat___’

Select * from customers where address LIKE ‘%TRAIL%’ OR address LIKE


‘%AVENUE%’ AND phone_num LIKE ‘%9’

REGEXP OPERATOR
^ beginning of a string
$ end of string
| logical or
[abc] match any of a,b,c
[a-z]

Select * from customers where firstname REGEXP ‘ELKA|AMBUR’


Select * from customers where lastname REGEXP ‘ey$|on$’
Select * from customers where lastname REGEXP ‘^my|se’
Select * from customers where lastname REGEXP ‘b[ru]’
NULL OPERATOR
Select * FROM order WHERE shipped_date IS NULL

ORDER BY CLAUSE
select * from customers order by state, name DESC

SELECT * quantity*unitprice AS totalprice


FROM Orderitems
WHERE orderid=2
ORDER BY totalprice DESC

LIMIT CLAUSE
SELECT * FROM customers LIMIT 3
SELECT * FROM customers LIMIT 6, 3
– - skip 6 record

SELECT *
FROM customers
ORDER BY points DESC
LIMIT 3

INNER JOINS
SELECT orderid, o.customerid, firstname, lastname
FROM orders o
INNER JOIN customer c
ON o.customerid = c.customerid

SELECT orderid, o.productid, p.name, quantity, o.unitprice


FROM orderitems o
INNER JOIN product p
ON o.productid = p.productid

SELF JOIN
SELECT *
FROM employees e
JOIN employees m
ON e.reports_to = m.employee_id

Joining multiple tables

SELECT p.invoiceid, p.date, client.name, pm.name


FROM payments p
JOIN clients c
ON p.clientid = c.clientid
JOIN payment_methods pm
ON pm.payment_method_id = p.payment_method

COMPOUND JOINS
Composite primary key
JOIN *****
ON ***
AND ***

Implicit JOIN Syntax


SELECT * FROM orders o, customers c WHERE o.id = c.id

OUTER JOINS
SELECT c.id, c.name, o.orderid
FROM customers c
LEFT JOIN orders o
ON c.id = o.orderid
ORDER BY c.id

– gives all custoners id and name whether they have an order or not
– Join = INNER JOIN
– LEFT or RIGHT JOIN = OUTER JOIN

SELECT p.productid, p.name, o.quantity


FROM product p
LEFT JOIN orders o
ON p.productid = orders.productid
SELECT
o.orderdate, o.orderid, c.name, sh.shippername, os.name AS status
FROM orders o
JOIN customer c
ON o.customerid = c.customerid
LEFT JOIN shippers sh
ON o.shipperid = sh.shipperid
JOIN orederstatus os
ON o.status = os.orderstatusid

SELF OUTER
SELECT *
FROM employees e
LEFT JOIN employees m
ON e.reports_to = m.employee_id

USING
If column name in different tables is same

SELECT orderid, o.customerid, c.firstname, sh.name AS shipper


FROM orders o
INNER JOIN customer c
USING (customerid)
LEFT JOIN shippers sh
USING (shipperid)

You might also like