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

4 - DML

This document provides a summary of SQL clauses and operators for querying and manipulating data in databases. It includes explanations and examples of SELECT statements using WHERE, IN, BETWEEN, LIKE, ORDER BY, LIMIT, DISTINCT, COUNT, aliases, NULL, logical operators, CASE, and INSERT, UPDATE, DELETE statements. The key clauses covered are for filtering rows by conditions, sorting result sets, returning subsets of data, temporarily renaming columns, and adding, modifying, or removing rows from tables.

Uploaded by

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

4 - DML

This document provides a summary of SQL clauses and operators for querying and manipulating data in databases. It includes explanations and examples of SELECT statements using WHERE, IN, BETWEEN, LIKE, ORDER BY, LIMIT, DISTINCT, COUNT, aliases, NULL, logical operators, CASE, and INSERT, UPDATE, DELETE statements. The key clauses covered are for filtering rows by conditions, sorting result sets, returning subsets of data, temporarily renaming columns, and adding, modifying, or removing rows from tables.

Uploaded by

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

WHERE & IN:

SELECT * FROM Customers -- or NOT type = 'gas'


WHERE type = 'gas';

SELECT * FROM Customers -- for more equal


WHERE type IN ('gas', 'electricity');
-----------------------------------------------------------------------------------
----------------------------------
= Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
<> Not equal to
-----------------------------------------------------------------------------------
----------------------------------
INSERT INTO Customers (CustomerName, ContactName, Address)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21');

INSERT INTO Customers -- for all columns


but sequenece should be same
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21');
-----------------------------------------------------------------------------------
----------------------------------
INSERT INTO Customers (CustomerName, City, Country) -- from one table to
another
SELECT SupplierName, City, Country FROM Suppliers;

INSERT INTO Customers (CustomerName, City, Country) -- with where


SELECT SupplierName, City, Country FROM Suppliers
WHERE Country='Germany';
-----------------------------------------------------------------------------------
----------------------------------
UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';

NOTE: Be careful when updating records. If you omit the WHERE clause, ALL records
will be updated!
-----------------------------------------------------------------------------------
----------------------------------
DELETE FROM Customers
WHERE CustomerName='Alfreds';

DELETE FROM table_name; -- delete


all records.
-----------------------------------------------------------------------------------
----------------------------------
SELECT DISTINCT Country FROM Customers;

SELECT COUNT(DISTINCT Country) FROM Customers

SELECT * FROM Customers -- only 3 records


LIMIT 3;
-----------------------------------------------------------------------------------
----------------------------------
ALIASES:
-SQL aliases are used to give a table, or a column in a table, a temporary name.
-Aliases are often used to make column names more readable.
-An alias only exists for the duration of that query.

SELECT CustomerName AS Customer, ContactName AS [Contact Person]


FROM Customers;
-----------------------------------------------------------------------------------
----------------------------------
NULL:

SELECT CustomerName, ContactName, Address


FROM Customers
WHERE Address IS NULL;

SELECT CustomerName, ContactName, Address


FROM Customers
WHERE Address IS NOT NULL;
-----------------------------------------------------------------------------------
----------------------------------
SORTING:

SELECT * FROM Customers


ORDER BY Country ASC, CustomerName DESC;
-----------------------------------------------------------------------------------
----------------------------------
LOGICAL OPERATORS:

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' AND NOT Country='USA';
-----------------------------------------------------------------------------------
----------------------------------
LIKE:

WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least
2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at
least 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with
"o"

SELECT * FROM Customers


WHERE CustomerName LIKE 'a%';
-----------------------------------------------------------------------------------
----------------------------------
BETWEEN:

SELECT * FROM Products


WHERE Price NOT BETWEEN 10 AND 20;
-----------------------------------------------------------------------------------
----------------------------------
CASE:
-when we need to check more than 1 conditions.
-this is similar to IF-THEN-ELSE.

SELECT OrderID, Quantity,


CASE
WHEN Quantity > 30 THEN 'The quantity is greater than 30'
WHEN Quantity = 30 THEN 'The quantity is 30'
ELSE 'The quantity is under 30'
END AS QuantityText
FROM OrderDetails;

NOTE: A CASE statement can return only single column not multiple columns.

select
(CASE
WHEN type = 'GAS' THEN (id)
END) from contracts;
-----------------------------------------------------------------------------------
--------------

You might also like