Lab Manual PDF
Lab Manual PDF
Lab Manual PDF
Example: create table stud (ID int, Name varchar(50), Grade char, GPA float);
1
Example: desc stud;
8) How to insert values into Table (INSERT OPERATION)
SQL query: INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Examples: INSERT into stud (ID, Name, Grade, GPA) VALUES (1125, 'Beka','A',3.9);
9) How to display the Tables (SELECT OPRATIONS)
SQL query: select * from table_name;
OR SELECT column1, column2, ...
FROM table_name;
Examples: select * from stud;
Select ID, GPA from stud;
The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a
table, a column often contains many duplicate values; and sometimes you only want to list the
different (distinct) values.
2
➢ The DELETE statement is used to delete existing records in a table.
The following SQL creates a PRIMARY KEY on the "ID" column when the "Persons" table is
created:
➢ The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
➢ A FOREIGN KEY is a field (or collection of fields) in one table, that refers to
the PRIMARY KEY in another table.
➢ The table with the foreign key is called the child table, and the table with the primary key
is called the referenced or parent table.
3
➢ Notice that the "PersonID" column in the "Orders" table points to the "PersonID" column in
the "Persons" table.
➢ The "PersonID" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
➢ The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
➢ The FOREIGN KEY constraint prevents invalid data from being inserted into the foreign key
column, because it has to be one of the values contained in the parent table.
4
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
To create a FOREIGN KEY constraint on the "PersonID" column when the "Orders" table is
already created, use the following SQL:
Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in
descending order, use the DESC keyword.
ORDER BY Syntax
5
Example
The GROUP BY statement groups rows that have the same values into summary rows, like "find
the number of customers in each country"
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
OR
SELECT COUNT(CustomerID),Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;
The HAVING clause was added to SQL because the WHERE keyword cannot be used with
aggregate functions
HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
6
HAVING condition
ORDER BY column_name(s);
Example
The AND and OR operators are used to filter records based on more than one condition:
• The AND operator displays a record if all the conditions separated by AND are TRUE.
• The OR operator displays a record if any of the conditions separated by OR is TRUE.
AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
7
SQL Joins
A JOIN clause is used to combine rows from two or more tables, based on a
related column between them