SQL Lab
SQL Lab
Syntax:-
CREATE DATABASE databasename;
Example:-
Syntax:-
DROP DATABASE databasename;
Example:-
Syntax:-
DROP TABLE Table_name;
Example:-
Syntax:-
TRUNCATE TABLE Table_name;
Example:-
2.
• INSERT INTO Customers
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen
21', 'Stavanger');
SQL DELETE FROM WHERE Statement
• The DELETE statement is used to delete existing records in a
table.
Syntax:- Delete record for condition
DELETE FROM table_name
WHERE condition;
Syntax:- Delete all record
DELETE * FROM table_name;
..SQL DELETE FROM WHERE Statement
Example:-
1.
DELETE FROM Customers
WHERE CustomerName='AMIT';
2.
DELETE FROM Customers
SQL UPDATE SET WHERE Statement
• The UPDATE statement is used to modify the existing records in
a table.
Syntax:- Update record for condition
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Syntax:- Update all record
UPDATE table_name
SET column1 = value1;
..SQL UPDATE SET WHERE Statement
Example:- Update record for condition
UPDATE Customers
SET ContactName = ‘amit', City= ‘bareilly'
WHERE CustomerID = 1;
Example:- Update all record
UPDATE Customers
SET location=‘bareilly';
SQL SELECT Statement
• The SELECT statement is used to select data from a database.
• The data returned is stored in a result table, called the result-set.
Syntax:- select given column for condition record
SELECT column1, column2, ...
FROM table_name WHERE condition;
Syntax:- select given column of all record
SELECT column1, column2, ...
FROM table_name
Syntax:- select all column of all record
SELECT * FROM table_name;
…SQL SELECT Statement
Example:- select given column for condition
record
SELECT id,name
FROM student WHERE city=‘bareilly’;
Example:- select given column of all record
SELECT id, name
FROM student;
Example:- select all column of all record
SELECT * FROM student;
SQL SELECT DISTINCT Statement
• The SELECT DISTINCT statement is used to return only distinct
(different) values.
Syntax:-
SELECT DISTINCT column1, column2, ...
FROM table_name
WHERE condition;
Example:-
SELECT DISTINCT city FROM Customers;
SQL ORDER BY Keyword
• The ORDER BY keyword is used to sort the result-set in ascending or
descending order.
Syntax:-
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Example:-
SELECT * FROM Customers ORDER BY Country;
OR
SELECT * FROM Customers
ORDER BY Country DESC;
SQL AND, OR and NOT Operators
• The WHERE clause can be combined with AND, OR, and NOT operators
Syntax:- AND
SELECT column1, column2, ...FROM table_name
WHERE condition1 AND condition2 AND condition3
...;
Syntax:- OR
SELECT column1, column2, ...
FROM table_nameWHERE condition1 OR condition
2 OR condition3 ...;
Syntax:- NOT
SELECT column1, column2, ...FROM table_name
WHERE NOT condition;
The SQL SELECT TOP Clause
• The SELECT TOP clause is used to specify the number of records to
return.
Syntax:-
SELECT TOP number|percent column_name
FROM table_name
WHERE condition;
Example:-
SELECT TOP 3 * FROM Customers;
SELECT TOP 50 PERCENT * FROM Customers;
The SQL MIN() and MAX() Functions
• The MIN() function returns the smallest value of the selected
column.
• The MAX() function returns the largest value of the selected
column.
Syntax:-
SELECT min|max (column_name)
FROM table_name
WHERE condition;
Example:-
SELECT MIN/MAX(Price) AS SmallestPrice
FROM Products;
SQL COUNT(), AVG() and SUM() Functions
• The COUNT() function returns the number of rows that matches a
specified criteria.
• The AVG() function returns the average value of a numeric column.
• The SUM() function returns the total sum of a numeric column.
Syntax:-
SELECT COUNT/AVG/SUM (column_name)
FROM table_name
WHERE condition;
Example:-
SELECT COUNT(ProductID)FROM Products;
SELECT AVG/SUM(Price)FROM Products;
The SQL LIKE Operator
• The LIKE operator is used in a WHERE clause to search for a specified
pattern in a column.
• There are two wildcards used in conjunction with the LIKE operator:
• % - The percent sign represents zero, one, or multiple characters
• _ - The underscore represents a single character
Syntax:-
SELECT column1, column2, ...FROM table_name
WHERE columnN LIKE pattern;
Example:-
SELECT * FROM Customers
WHERE CustomerName LIKE '%a';
WHERE CustomerName LIKE 'a%' Finds any values that starts with "a"
WHERE CustomerName LIKE '%a' Finds any values that ends 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 starts with "a" and are
at least 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that starts with "a" and
ends with "o"
SQL Wildcard Characters
• A wildcard character is used to substitute any
other character(s) in a string.
• % - The percent sign represents zero, one, or
multiple characters
• _ - The underscore represents a single character
• [charlist] - Defines sets and ranges of characters
to match
• [^charlist] or [!charlist] - Defines sets and ranges
of characters NOT to match
• The wildcards can also be used in combinations!
SQL Wildcard Characters
Example:-
SELECT * FROM Customers
WHERE City LIKE 'ber%';