0% found this document useful (0 votes)
17 views

SQL Notes

This document provides examples of SQL queries using SELECT, WHERE, ORDER BY, LIMIT, LIKE, BETWEEN and other clauses. It shows how to select data from tables, filter using conditions, sort the results, limit the number of rows, and use wildcards and ranges to search for patterns in string fields. Aggregate functions like COUNT, MIN, MAX, AVG and SUM are also demonstrated.

Uploaded by

warowic
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

SQL Notes

This document provides examples of SQL queries using SELECT, WHERE, ORDER BY, LIMIT, LIKE, BETWEEN and other clauses. It shows how to select data from tables, filter using conditions, sort the results, limit the number of rows, and use wildcards and ranges to search for patterns in string fields. Aggregate functions like COUNT, MIN, MAX, AVG and SUM are also demonstrated.

Uploaded by

warowic
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

## SQL notes referenced from w3schools.

com ##
---------------------------------------------

SELECT * FROM customers;


SELECT country FROM customers;

SELECT DISTINCT country FROM customers;


SELECT DISTINCT * FROM customers;

SELECT COUNT(country) FROM customers;


SELECT COUNT(DISTINCT country) FROM customers;

SELECT * FROM customers WHERE country = "Mexico";


SELECT * FROM customers WHERE customer_id = 1;

SELECT * FROM products WHERE price = 30;


SELECT * FROM products WHERE price >= 30;
SELECT * FROM products WHERE price <= 30;
SELECT * FROM products WHERE price < 30;
SELECT * FROM products WHERE price > 30;
SELECT * FROM products WHERE price <> 30; ---(not equals)
SELECT * FROM products WHERE price != 30; ---(not equals)
SELECT * FROM products WHERE price BETWEEN 50 AND 60;

SELECT * FROM Customers WHERE City LIKE 'a%';


---(returns all entries in 'city' starting with 'a')

SELECT * FROM Customers WHERE City LIKE 's%';


---(returns all entries in 'city' starting with 's')

SELECT * FROM Customers WHERE City LIKE 'r%';


---(returns all entries in 'city' starting with 'r')

SELECT * FROM Customers WHERE City IN ('Paris','London');


---(return all either 'Paris' or 'London')

[<ORDER BY> keyword -- used for sorting]

SELECT * FROM Customers ORDER BY city;


---(return according to city name in ascending order)

SELECT * FROM Customers ORDER BY Country DESC;


---(return according to country name in descending order)

SELECT * FROM Customers ORDER BY Country ASC;


---(return according to country name in descending order)

SELECT * FROM Customers ORDER BY Country, CustomerName;


---(returns acc. to Country name, but if some clash, it sorts those according to
CustomerName)

SELECT * FROM Customers ORDER BY Country ASC, CustomerName DESC;


INSERT INTO customers (customerID, customerName, age) VALUES ('23','Payal','20');

SELECT * FROM Customers WHERE Address IS NULL;


SELECT CustomerName, ContactName, Address FROM Customers WHERE Address IS NULL;
SELECT * FROM customers WHERE address IS NOT NULL;

UPDATE mytab1 SET Roll_no='75', name='lekhi', marks='88' WHERE roll_no=1;

UPDATE Customers SET PostalCode = 00000 WHERE Country = 'Mexico';

UPDATE Customers SET PostalCode = 00000;


---(Be Careful, if there is no WHERE, it will update all the records)

DELETE FROM Customers WHERE CustomerName='Alex';


DELETE FROM Customers; ---(deletes all records of 'Customers')

SELECT * FROM Customers LIMIT 50;


SELECT * FROM Customers WHERE Country='Germany' LIMIT 10;

SELECT MIN(Price) FROM Products;


SELECT MIN(Price) AS smallest_price FROM Products; ---(entity name)
SELECT MAX(Price) AS largest_price FROM Products;

SELECT COUNT(price) FROM products;


SELECT AVG(price) FROM products;
SELECT SUM(price) FROM products;

[LIKE operator - used for searching patterns]


[ % - represents zero or more characters ]
[ * - represents single character ]

SELECT * FROM Customers WHERE City LIKE 'a%'; ---(starts with 'a')
SELECT * FROM customers WHERE city LIKE '%a'; ---(ends with 'a')
SELECT * FROM customers WHERE city LIKE '%ar%'; ---(has 'ar' anywhere)
SELECT * FROM customers WHERE city LIKE '_o%'; ---(has 'o' at 2nd position)
SELECT * FROM customers WHERE city LIKE 'h%r'; ---(starts with 'h' and ends with
'r')
SELECT * FROM customers WHERE city LIKE 'a__%'; ---(starts with 'a', and has
atleast 3 characters in it)

SELECT * FROM customers WHERE country IN ('india','usa');


SELECT * FROM customers WHERE country IN (SELECT country FROM suppliers);
SELECT * FROM customers WHERE country NOT IN ('Germany','Japan', 'UK');

SELECT * FROM Products WHERE Price BETWEEN 10 AND 20;


SELECT * FROM Products WHERE Price NOT BETWEEN 10 AND 20;
SELECT * FROM Products WHERE ProductName BETWEEN 'Carnarvon Tigers' AND 'Mozzarella
di Giovanni'; ---(BETWEEN can also have string values)
[Aliases - (AS) used for that query only, to make data more readable]
SELECT CustomerID AS id FROM Customers;
SELECT CustomerID AS id, CustomerName AS cust FROM Customers;

SELECT CustomerName, CONCAT_WS(', ', Address, PostalCode, City, Country) AS Address


FROM Customers;
---(this query creates an alias named "Address" that combine four columns:
'Address', 'PostalCode', 'City', 'Country')

You might also like