1)To get all items from the table:
SELECT * FROM tablename;
2)To get specific fields from the table:
SELECT CustomerName, City FROM Customers;
3)Select distinct values for a column from a table:
SELECT DISTINCT Country FROM Customers;
4)Get the count of distinct values for a column from a table:
SELECT COUNT(DISTINCT Country) FROM Customers;
5)Where conditions to get data from the table:
SELECT * FROM Customers
WHERE Country='Mexico';
6)Where with and and or ,not :
SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';
SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
SELECT * FROM Customers
WHERE NOT Country='Germany';
7)Sorting by default ascending
SELECT * FROM Customers
ORDER BY Country;
SELECT * FROM Customers
ORDER BY Country DESC;
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
8)Insert
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
9)NUll Values:
SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NULL;
SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NOT NULL;
Comments
Post a Comment