SQL Assignment 01
SQL Assignment 01
Objective:
To practice and write SQL queries using Data Definition Language (DDL) and Data Manipulation
Language (DML) commands.
What is SQL?
● SQL stands for Structured Query Language. It is used for storing and managing data in
the Relational Database Management System (RDBMS).
● It is a standard language for Relational Database Systems. It enables a user to
create, read, update and delete relational databases and tables.
● All the RDBMS like MySQL, Informix, Oracle, MS Access and SQL Server use
SQL as their standard database language.
● SQL allows users to query the database in a number of ways, using
English-like statements.
When an SQL command is executed in any Relational Database Management System (RDBMS),
the system processes the request to find the most efficient way to perform the task. The SQL
engine plays a key role in determining how to interpret and execute the query.
Advantages of SQL:
● High Speed: SQL queries are optimized for performance, allowing for fast data retrieval
and manipulation.
● No Coding Needed: SQL uses declarative language, meaning users specify what they
want without having to write detailed procedural code.
● Well-Defined Standards: SQL follows standardized rules and syntax, ensuring
consistency across different RDBMS systems.
● Portability: SQL commands are largely portable across various database systems, making
it easier to switch or integrate different systems.
● Interactive Language: SQL allows for interactive data querying and manipulation,
making it suitable for real-time data access and analysis.
● Multiple Data Views: SQL supports creating various views and abstractions of data,
allowing users to see and interact with data in different ways.
● SQL Datatype is used to define the values that a column can contain.
● Every column is required to have a name and data type in the database table.
Commonly used Data Types
● INT: An integer data type used to store whole numbers.
● VARCHAR(n): A variable-length string data type that can store up to `n` characters.
● CHAR(n): A fixed-length string data type that always stores exactly `n` characters,
padding with spaces if necessary.
● TEXT: A variable-length string data type used to store large amounts of text.
● DATE: A data type used to store calendar dates (year, month, and day).
● TIME: A data type used to store time of day (hours, minutes, seconds).
● DATETIME: A data type used to store both date and time information.
● TIMESTAMP: A data type used to store a combination of date and time, typically with
timezone information.
● FLOAT: A floating-point number data type used to store approximate numeric values
with fractional components.
● DECIMAL(p, s): A fixed-point number data type with precision `p` (total number of
digits) and scale `s` (number of digits to the right of the decimal point).
● BOOLEAN: A data type used to store true or false values.
SQL Commands
• SQL commands are instructions. It is used to communicate with the database. It is also
used to perform specific tasks, functions, and queries of data.
• SQL can perform various tasks like create a table, add data to tables, drop the table,
modify the table, set permission for users.
There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
Data Definition Language (DDL)
1. DDL changes the structure of the table like creating a table, deleting a table, altering a
table, etc.
2. All the commands of DDL are auto-committed, which means it permanently saves all
the changes in the database.
3. Here are some commands that come under DDL:
i. CREATE
ii. ALTER
iii. DROP
iv. TRUNCATE
Grade CHAR(1) );
Syntax:
DROP TABLE table_name;
Example:
DROP TABLE classroom;
DML commands are used to manage and manipulate the data stored in a database. Unlike Data
Definition Language (DDL) commands, which affect the structure of the database, DML
commands are focused on modifying the data within the tables.
Characteristics:
INSERT: The INSERT statement is a SQL query. It is used to insert data into the row of a
table.
Syntax 1:
INSERT INTO <Table_Name>(attr1, attr2, attr3) values(att_vl1, attr_val2, att_vl3);
INSERT INTO table_name
(column1, column2, ...)
VALUES (value1, value2, ...);
1 CSE Hyderabad
2 ECE KGP
3 CIVIL Jaipur
Example:
INSERT INTO Departments (Dept_id, Dept_Name, Location)
VALUES (1, 'CSE', 'Hyderabad'), (2, 'ECE', 'KGP'), (3, 'CIVIL', 'Jaipur');
Syntax 2:
INSERT INTO <Table_Name> values(att_vl1, attr_val2…);
INSERT INTO table_name
VALUES (value1, value2, ...);
Update: This command is used to update or modify the value of a column in the table.
Syntax 1:
UPDATE <Table_name> SET <attribute> = <value>
WHERE <Condition>
UPDATE table_name
SET column1 = value1, column2 = value2, …
WHERE condition;
Example:
UPDATE Dept SET dept_name = 'IT'
WHERE location = 'Jaipur' ;
DQL is used to retrieve or fetch data from the database. It consists of the SELECT statement,
which is used to query and extract data based on certain conditions.
Command: SELECT
SQL Operators
SQL operators are used to specify conditions in SQL statements and to perform operations on
data in tables.
1. Arithmetic Operators
+ Addition SELECT 5 + 3;
- Subtraction SELECT 5 - 3;
* Multiplication SELECT 5 * 3;
/ Division SELECT 6 / 3;
2. Comparison Operators
> Greater than SELECT * FROM table WHERE age > 30;
< Less than SELECT * FROM table WHERE age < 30;
>= Greater than or equal to SELECT * FROM table WHERE age >= 30;
<= Less than or equal to SELECT * FROM table WHERE age <= 30;
BETWEEN Between an inclusive SELECT * FROM table WHERE age
range BETWEEN 20 AND 30;
IN Match any value in a SELECT * FROM table WHERE age IN (20, 30,
set 40);
3. Logical Operators
AND Returns true if all conditions SELECT * FROM table WHERE age > 20
are true AND salary > 50000;
OR Returns true if at least one SELECT * FROM table WHERE age > 30
condition is true OR salary > 50000;
NOT Returns true if the condition is SELECT * FROM table WHERE NOT age >
false 30;
4. IS NULL Operator
IS NOT Checks if a value is not SELECT * FROM table WHERE age IS NOT
NULL NULL NULL;
5. LIKE Operator
LIKE Searches for a pattern SELECT * FROM table WHERE name LIKE 'A%';
(names starting with 'A')
% Represents zero or more SELECT * FROM table WHERE name LIKE '%son';
characters (like 'Jackson', 'Johnson')
6. Set Operators
UNION Combines the result sets and SELECT column FROM table1
removes duplicates UNION SELECT column FROM
table2;
UNION ALL Combines the result sets and SELECT column FROM table1
includes duplicates UNION ALL SELECT column
FROM table2;
INTERSECT Returns only the common results Not supported in MySQL
1. Write the SQL statement to create the World table with the following schema:
a. name (VARCHAR, primary key)
b. continent (VARCHAR)
c. area (INT)
d. population (INT)
e. gdp (BIGINT)
f. Insert Data:
2. Insert the following data into the World table:
a. ('Afghanistan', 'Asia', 652230, 25500100, 20343000000)
b. ('Albania', 'Europe', 28748, 2831741, 12960000000)
c. ('Algeria', 'Africa', 2381741, 37100000, 188681000000)
d. ('Andorra', 'Europe', 468, 78115, 3712000000)
e. ('Angola', 'Africa', 1246700, 20609294, 100990000000)
3. Write a query to retrieve all the data from the World table.
4. Write a query to retrieve only the name, population, and area columns from the
World table.
5. Write a query to retrieve the distinct continents present in the World table.
6. Write a query to find the name, population, and area of all countries that are
either:
a. Bigger than or equal to 3,000,000 km² or
b. Have a population greater than or equal to 25,000,000.
7. Write a query to update the population of 'Angola' to 21,000,000.
8. Write a query to delete the row for 'Andorra' from the World table.
9. Write a query to drop the World table.