Skip to content

Instantly share code, notes, and snippets.

@ola-dola
Created July 13, 2021 19:01
Show Gist options
  • Save ola-dola/adc5336495ecadb20e4242ef8e1af57d to your computer and use it in GitHub Desktop.
Save ola-dola/adc5336495ecadb20e4242ef8e1af57d to your computer and use it in GitHub Desktop.
Example SQL queries for common tasks. Run query in browser: https://www.w3schools.com/Sql/tryit.asp?filename=trysql_select_top
SELECT * FROM Customers
WHERE city like "M%"; // cities that start with M.
select * FROM Customers
where city like "B%" AND customerId >= 10;
select * FROM Customers
where city like "B%" OR customerId >= 10;
select * FROM Customers
where city like "B%" or customerId >= 10
order by city desc;
select * FROM Customers
where city like "B%" or customerId >= 10
order by city desc
limit 5;
INSERT into Shippers (phone, shipperName)
values ("234-567-0987", "Natinale Mouvier");
SELECT * FROM [Shippers]
where shippername is null; // "is" is how we filter for null. not =, not "like"
UPDATE shippers
set phone = "Alamale"
where shipperId="4"; // Dont forget the where clause for update and delete operations
DELETE from Shippers
where ShipperID = 4;
SELECT * FROM Customers
where
City IN ("London", "Madrid")
OR Country = "Brazil";
INSERT into Customers
(CustomerName, ContactName, Address, City, PostalCode, Country)
values
("The Shire", "Bilbo Baggins", "1 Hobbit-Hole", "Bag End", "111", "Middle Earth");
UPDATE Customers
set PostalCode = 11122
where CustomerId = 92;
SELECT COUNT (DISTINCT city) AS "Number of Cities" FROM Customers;
SELECT COUNT(SupplierName) FROM [Suppliers]
where length(SupplierName) > 20;
SELECT * FROM [Suppliers]
where length(SupplierName) > 20;
SELECT P.ProductID, P.ProductName, P.Price, C.CategoryName
FROM Products as P
join categories as C
on C.categoryID = P.categoryID
join Suppliers as S
on S.SupplierID = P.supplierID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment