Unit II SQL
Unit II SQL
Unit II SQL
SELECT
1 Retrieves certain records from one or more tables.
INSERT
2 Creates a record.
UPDATE
3 Modifies records.
DELETE
4 Deletes records.
DCL - Data Control Language
GRANT
1 Gives a privilege to user.
REVOKE
2 Takes back privileges granted from user.
RDBMS:
MEDIUMBLOB For BLOBs (Binary Large OBjects). Holds up to 16,777,215 bytes of data
LONGBLOB For BLOBs (Binary Large OBjects). Holds up to 4,294,967,295 bytes of data
ENUM(val1, val2, ...) A string object that can have only one value, chosen from a list of possible
values. You can list up to 65535 values in an ENUM list. If a value is inserted that
is not in the list, a blank value will be inserted. The values are sorted in the
order you enter them
SET(val1, val2, ...) A string object that can have 0 or more values, chosen from a list of possible
values. You can list up to 64 values in a SET list
Data type Description
BIT(size) A bit-value type. The number of bits per value is specified in size. The size parameter can hold a
value from 1 to 64. The default value for size is 1.
TINYINT(size) A very small integer. Signed range is from -128 to 127. Unsigned range is from 0 to 255.
The size parameter specifies the maximum display width (which is 255)
INT(size) A medium integer. Signed range is from -2147483648 to 2147483647. Unsigned range is from 0 to
4294967295. The size parameter specifies the maximum display width (which is 255)
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
Example: The following SQL statement selects all customers with a ContactName
that starts with "a" and ends with "o":
SELECT * FROM Customers
WHERE ContactName LIKE 'a%o';
Example: The following SQL statement selects all customers with a CustomerName
that does NOT start with "a":
SELECT * FROM Customers
WHERE CustomerName NOT LIKE 'a%';
Example: The following SQL statement selects all customers with a CustomerName
that starts with "a" and are at least 3 characters in length:
SELECT * FROM Customers
WHERE CustomerName LIKE 'a_ _%';
LIKE Operator Description
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"
The SQL IN Operator
• The IN operator allows you to specify multiple values in a WHERE clause.
• The IN operator is a shorthand for multiple OR conditions.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
OR:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
Continued…
Example: SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
Example: SELECT * FROM Customers
WHERE Country NOT IN ('Germany', 'France', 'UK');
Example: SELECT * FROM Customers
WHERE Country IN (SELECT Country FROM Suppliers);
(Selects all customers that are from the same countries as the
suppliers)
The SQL BETWEEN Operator
• The BETWEEN operator selects values within a given range. The values can be
numbers, text, or dates.
• The BETWEEN operator is inclusive: begin and end values are included.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Example: SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
Example: SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
BETWEEN with IN Example
• The following SQL statement selects all products with a price
BETWEEN 10 and 20. In addition; do not show products with a
CategoryID of 1,2, or 3:
Example: SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20
AND CategoryID NOT IN (1,2,3);
• The following SQL statement selects all products with a ProductName
BETWEEN Carnarvon Tigers and Mozzarella di Giovanni:
Example: SELECT * FROM Products
WHERE ProductName BETWEEN 'Carnarvon Tigers' AND 'Mozzarella di
Giovanni'
ORDER BY ProductName;
Continued…
10308 1 1996-09-18
1 Arif Maria Germany
10309 2 1996-09-19 2 Alok Ana Mexico
10310 3 1996-09-20 3 Raju Antonio Mexico
Example: The following SQL statement copies "Suppliers" into "Customers" (the columns that are
not filled with data, will contain NULL):
INSERT INTO Customers (CustomerName, City, Country)
SELECT SupplierName, City, Country FROM Suppliers;
Example: The following SQL statement copies "Suppliers" into "Customers" (fill all columns):
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
SELECT SupplierName, ContactName, Address, City, PostalCode, Country FROM Suppliers;
Example: The following SQL statement copies only the German suppliers into "Customers":
INSERT INTO Customers (CustomerName, City, Country)
SELECT SupplierName, City, Country FROM Suppliers
WHERE Country='Germany';
The SQL CASE Statement
• The CASE statement goes through conditions and returns a value when the first
condition is met (like an IF-THEN-ELSE statement). So, once a condition is true, it
will stop reading and return the result. If no conditions are true, it returns the
value in the ELSE clause.
• If there is no ELSE part and no conditions are true, it returns NULL.
Syntax:
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
Example: It returns a value when the first condition met.
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;
Example: The following SQL will order the customers by City.
SELECT CustomerName, City, Country
FROM Customers
ORDER BY
(CASE
WHEN City IS NULL THEN Country
ELSE City
END);
References
1. https://www.tutorialspoint.com/sql/index.htm
2. https://www.geeksforgeeks.org/sql-tutorial/
3. https://www.w3schools.com/sql/