Lab Manual (DDS)
Lab Manual (DDS)
Submitted By:
Roll No:
586403
Class:
BS (Computer Science)
Semester:
6th
Session:
2020 – 2024
Course Title:
SQL:
SQL Work:
SQL Statements:
Most of the actions you need to perform on a database are done with SQL statements. SQL
statements consist of keywords that are easy to understand.
Syntax:
SELECT colum1, colum2, ………..
FROM table_name;
Example:
Return all columns from Customer table
Syntax:
UPDATE table_name
WHERE condition;
Example:
UPDATE Customers
WHERE CustomerID = 1;
Syntax:
DELETE FROM table_name WHERE condition;
Example:
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';
Syntax:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Example:
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
SQL Operators: -
Operator Description
The AND operator is used to filter records based on more than one condition, like if you want to
return all customers from Spain that starts with the letter 'G':
Example:
Select all customers from Spain that starts with the letter 'G':
Syntax:
The OR operator is used to filter records based on more than one condition, like if you want to
return all customers from Germany but also those from Spain:
Example:
Syntax:
In the select statement below we want to return all customers that are NOT from Spain:
Example:
Syntax:
SELECT column1, column2, ... FROM table_name
(INNER) JOIN
LEFT (OUTER) JOIN
RIGHT (OUTER) JOIN
FULL (OUTER) JOIN
The INNER JOIN keyword selects records that have matching values in both tables.
Example:
Join Products and Categories with the INNER JOIN keyword:
SELECT ProductID, ProductName, CategoryName
FROM Products
INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID;
Syntax:
SELECT column_name(s)
FROM table1
ON table1.column_name = table2.column_name;
2.SQL LEFT JOIN Keyword:
The LEFT JOIN keyword returns all records from the left table (table1), and the matching
records from the right table (table2). The result is 0 records from the right side, if there is no
match.
Syntax:
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Syntax:
SELECT column_name(s)
FROM table1
ON table1.column_name = table2.column_name;
Example:
FROM Orders
ORDER BY Orders.OrderID;
3.SQL FULL OUTER JOIN Keyword:
The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right
(table2) table records.
Syntax:
SELECT column_name(s)
FROM table1
ON table1.column_name = table2.column_name
WHERE condition;
Example:
FROM Customers
ORDER BY Customers.CustomerName;
4.SQL UNION Operator:
The UNION operator is used to combine the result-set of two or more SELECT statements.
UNION Syntax:
UNION
Example:
UNION
ORDER BY City;
HAVING Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
FROM Customers
GROUP BY Country
ANY Syntax:
SELECT column_name(s)
FROM table_name
(SELECT column_name
FROM table_name
WHERE condition);
ALL Syntax:
FROM table_name
WHERE condition;
FROM Products
FROM OrderDetails
FROM Products
(SELECT ProductID
FROM OrderDetails
Syntax:
Example:
Syntax:
DROP DATABASE databasename;
Example:
Example:
TO DISK = 'D:\backups\testDB.bak';
SQL CREATE TABLE Statement:
The CREATE TABLE statement is used to create a new table in a database.
Syntax:
column1 datatype,
column2 datatype,
column3 datatype,
....
);
Example
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
1.DROP TABLE Statement:
The DROP TABLE statement is used to drop an existing table in a database.
Syntax:
Example:
Syntax:
....
);
SQL References: -
Function Description
Chr Returns the character for the specified ASCII number code
Function Description
Function Description