DBMS Lab Manual Program 1 To 10
DBMS Lab Manual Program 1 To 10
Program-1
Functionality of SQL:
1. Data Querying: SQL allows for complex queries to retrieve specific data from databases
using the SELECT statement. It supports filtering (WHERE), sorting (ORDER BY),
grouping (GROUP BY), and joining data across multiple tables (JOIN).
2. Data Insertion: SQL allows inserting new records into tables using the INSERT INTO
command.
3. Data Updating: SQL can modify existing records using the UPDATE command.
4. Data Deletion: SQL allows removing records from a table using the DELETE command.
5. Database and Table Management: SQL supports creating, altering, and dropping tables,
views, indexes, and databases, allowing flexible database structure management.
6. Indexing: SQL allows for the creation of indexes to improve the performance of data
retrieval.
7. Constraints and Relationships: SQL can define constraints (such as PRIMARY KEY,
FOREIGN KEY, UNIQUE, CHECK, and NOT NULL) to enforce data integrity and
relationships between tables.
Characteristics of SQL:
1. Declarative Language: SQL allows users to specify what they want to do with the data
without defining how to accomplish it. This simplifies database interaction by focusing on
"what" rather than "how."
2. Data Definition Language (DDL): SQL includes commands to define database structures,
such as creating, altering, and dropping tables or databases (CREATE, ALTER, DROP,
TRUNCATE).
Commands:
CREATE: Used to create new database objects such as tables, indexes, or views.
                                              1
                                                                    Reg. No. – 11023210098
                                                                              Section – “B”
Date – 11th Nov 2024                                                Name – Aayush Mehta
TRUNCATE: Removes all records from a table but keeps the structure.
3. Data Manipulation Language (DML): SQL is used to manipulate data within tables,
allowing users to insert, update, delete, or query records (SELECT, INSERT, UPDATE,
DELETE).
Commands:
SELECT: Retrieves data from the database.
                                             2
                                                                    Reg. No. – 11023210098
                                                                              Section – “B”
Date – 11th Nov 2024                                            Name – Aayush Mehta
4. Data Control Language (DCL): SQL includes commands to control access to data, such
as granting or revoking permissions (GRANT, REVOKE).
Commands:
GRANT: Provides access privileges to the users.
Commands:
COMMIT: Saves all changes made in the transaction to the database.
Program-2
Aim  To study Basic SQL commands (create database, create table, use, drop, insert,
delete, show) and execute the queries using these commands.
Pre-Questions:
                                            4
                                                                 Reg. No. – 11023210098
                                                                           Section – “B”
Date – 11th Nov 2024                                                   Name – Aayush Mehta
       Examples of RDBMS:
        - MySQL
        - PostgreSQL
        - Oracle Database
        - Microsoft SQL Server
        - SQLite
       Example of a table:
          CustomerID          Name                 Address          PhoneNumber
          1                   Aayush               Karnal           1234567890
          2                   Payal                Panipat          1234567891
In this example, CustomerID, Name, Address, and PhoneNumber are fields (columns), while
the data for each customer forms the rows (records).
                                               5
                                                                      Reg. No. – 11023210098
                                                                                Section – “B”
Date – 11th Nov 2024                                           Name – Aayush Mehta
                                                6
                                                               Reg. No. – 11023210098
                                                                         Section – “B”
Date – 11th Nov 2024                                                 Name – Aayush Mehta
                                                  7
                                                                     Reg. No. – 11023210098
                                                                               Section – “B”
Date – 11th Nov 2024                             Name – Aayush Mehta
                                             8
                                                 Reg. No. – 11023210098
                                                           Section – “B”
Date – 11th Nov 2024                                               Name – Aayush Mehta
Post-Questions:
   1) What is the difference between Char and Varchar 2 in data type in SQL?
      Ans- CHAR: It is a fixed-length data type. If you define a column as CHAR(10) and
      store a 5-character word, SQL will pad the remaining space with spaces until the
      length is 10 characters.
      VARCHAR2: It is a variable-length data type. When you define a column as
      VARCHAR2(10) and store a 5-character word, only 5 characters are stored without
      padding the rest.
   3) What is schema?
      Ans- A schema in SQL is a logical collection of database objects, such as tables,
      views, procedures, etc., owned by a database user. It helps organize and manage data
      0structures within the database.
Program-3
Aim  To study the viewing commands (select, update) and execute the queries using these
commands.
Pre-Questions:
   1) What is Clause?
      Ans- A clause in SQL is a part of a query that specifies a condition or modifies the
      behaviour of a statement. Clauses typically work in conjunction with SQL commands
      such as SELECT, INSERT, UPDATE, and DELETE. Examples of clauses include:
                                            9
                                                                   Reg. No. – 11023210098
                                                                             Section – “B”
Date – 11th Nov 2024                                                Name – Aayush Mehta
ORDER BY: Used to sort the result set in ascending or descending order.
       GROUP BY: Used to group rows sharing a property so that aggregate functions can
       be applied.
   2) What is Sub-Query?
      Ans- A sub-query (also known as an inner query or nested query) is a query within
      another query. A sub-query is usually enclosed within parentheses and is used to
      retrieve data that will be used by the main query. Sub-queries are commonly used
      with SELECT, INSERT, UPDATE, or DELETE statements to perform more complex
      operations.
   3)
   What is an Alias?
      Ans- An alias in SQL is a temporary name assigned to a table or column. It makes the
      SQL query more readable and can be used to shorten table or column names in the
      output or in complex queries.
       Table Alias: Assigns a temporary name to a table, often used in joins to make the
       query more concise.
Insertion of data:
                                             10
                                                                   Reg. No. – 11023210098
                                                                             Section – “B”
Date – 11th Nov 2024                                           Name – Aayush Mehta
Write a SQL query for emp_no and f_name where salary > 5000:
Write a SQL query for emp_no and doh for all hiring:
Write a SQL query to update salary to 10000 whose salary < 5000:
                                         11
                                                              Reg. No. – 11023210098
                                                                        Section – “B”
Date – 11th Nov 2024                                             Name – Aayush Mehta
Write a SQL query to update l_name and city of employee whose job_id is AB-123:
Post-Questions:
   1) Write an SQL query to fetch current date and time from the system?
      Ans- In MySQL, you can fetch the current date and time using the NOW() function:
                                          12
                                                                Reg. No. – 11023210098
                                                                          Section – “B”
Date – 11th Nov 2024                                                 Name – Aayush Mehta
      The DELETE command is used to remove specific rows from a table based on a
      condition.
      It removes rows one at a time, and the action can be rolled back (if within a
      transaction).
Example:
TRUNCATE:
The TRUNCATE command is used to remove all rows from a table quickly.
      The operation cannot be rolled back (in most databases), and it also resets any auto-
      increment counters.
Example:
Key Differences:
DELETE is slower and can be used with conditions; it can be rolled back.
      TRUNCATE is faster and removes all rows without conditions; it cannot be rolled
      back in most cases.
                                             13
                                                                    Reg. No. – 11023210098
                                                                              Section – “B”
Date – 11th Nov 2024                                                  Name – Aayush Mehta
       Structured: SQL databases are relational and store data in tables with predefined
       schemas (structured data).
       Query Language: SQL databases use Structured Query Language (SQL) for defining
       and manipulating data.
NoSQL Databases:
       Flexible Schema: They provide more flexibility with the structure of data, making
       them suitable for unstructured or semi-structured data.
Program-4
Aim  To study the commands to modify the structure of table (alter, delete, drop, add,
modify) and execute the queries using these commands.
Pre-Questions:
1)What is a View and Why we use it?
Ans- A view in SQL is a virtual table that is based on the result of a SELECT query. It doesn't
store data physically but provides a way to look at the data in one or more tables. Views are
used to simplify complex queries, provide security (by restricting access to certain data), and
present data in a specific format.
Usage of View:
                                              14
                                                                     Reg. No. – 11023210098
                                                                               Section – “B”
Date – 11th Nov 2024                                                     Name – Aayush Mehta
                                               15
                                                                        Reg. No. – 11023210098
                                                                                  Section – “B”
Date – 11th Nov 2024        Name – Aayush Mehta
Insertion of data:
                       16
                            Reg. No. – 11023210098
                                      Section – “B”
Date – 11th Nov 2024                                           Name – Aayush Mehta
Write a SQL query to add a column m_id with default value a-101:
Write a SQL query to change data type of doh (from date to datetime):
Post-Questions:
1) What is constraint in SQL? Name few of them.
Ans- A constraint in SQL is a rule applied to a column or a set of columns in a table to
enforce data integrity, ensuring the accuracy and reliability of the data in the database.
Constraints help ensure that the values in a column comply with certain conditions.
Common types of SQL constraints:
PRIMARY KEY: Ensures that a column or a combination of columns has unique values and
cannot be NULL.
FOREIGN KEY: Ensures referential integrity between two tables by making sure a
column’s values correspond to valid values in another table.
UNIQUE: Ensures that all values in a column are unique (except NULL).
NOT NULL: Ensures that a column cannot have NULL values.
CHECK: Ensures that all values in a column satisfy a specific condition.
DEFAULT: Sets a default value for a column when no value is provided during an insert
operation.
Cannot be used with aggregate functions like SUM(), COUNT(), AVG(), etc
HAVING:
Filters rows after grouping or aggregation has taken place.
Used in conjunction with the GROUP BY clause.
Can be used with aggregate functions.
2. One-to-Many (1:N):
Each row in Table A is related to one or more rows in Table B, but each row in Table B is
related to only one row in Table A.
Example: A customer can have many orders, but an order is placed by only one customer.
                                              19
                                                                      Reg. No. – 11023210098
                                                                                Section – “B”
Date – 11th Nov 2024                                                 Name – Aayush Mehta
3. Many-to-Many (N:N):
Each row in Table A can be related to multiple rows in Table B, and each row in Table B can
be related to multiple rows in Table A. This is often implemented using a junction table (a
table that connects the two tables).
Example: A student can enrol in multiple courses, and a course can have multiple students.
                                             20
                                                                    Reg. No. – 11023210098
                                                                              Section – “B”
Date – 11th Nov 2024                                                 Name – Aayush Mehta
Program-5
Aim  To study the commands that involve compound conditions (and, or, in, not in,
between, not between, like, not like) and execute the queries using these commands.
Pre-Questions:
   1) Difference between SQL, MySQL and SQL Server?
      Ans-
        SQL (Structured Query         MySQL:                      SQL Server (Microsoft
        Language):                                                SQL Server):
                                      MySQL is an open-source
        SQL is a standard             relational database         SQL Server is a relational
        programming language          management system           database management
        used to manage and            (RDBMS) that uses SQL       system developed by
        manipulate relational         as its language for         Microsoft. Like MySQL,
        databases. It is a language   querying data.              SQL Server also uses
        that provides commands                                    SQL to manage and
        for querying, updating,       MySQL is widely used        manipulate data.
        and managing data in          for web applications and
        databases.                    supports SQL to manage      It is often used in
                                      data.                       enterprise environments
        SQL itself is not a                                       with .NET applications
        database, but rather the      It was developed by         and integrates well with
        language used to interact     Oracle Corporation and is   Microsoft services.
        with databases.               commonly used with PHP
                                      and Linux in the LAMP       It supports features like
        Common SQL                    stack.                      advanced analytics,
        commands: SELECT,                                         integration services, and
        INSERT, UPDATE,                                           business intelligence.
        DELETE, CREATE,
        DROP, ALTER, etc.
                                              21
                                                                    Reg. No. – 11023210098
                                                                              Section – “B”
Date – 11th Nov 2024                                                 Name – Aayush Mehta
Insertion of data:
Write a SQL query about customer with their c_id, c_name, city where grade value is
100 or 300.
                                             22
                                                                    Reg. No. – 11023210098
                                                                              Section – “B”
Date – 11th Nov 2024                                            Name – Aayush Mehta
Write a SQL query for c_id, c_name, s_id of the customer who lives in New York city
and have grade is above 100.
Write a SQL query for list of customers from California and whose grade is either 200
or 400.
Write a SQL query for table name customer where s_id is 1001, 1005, 1006, 1010.
Write a SQL query for table name customer where s_id is not 1001, 1005, 1006, 1010.
Write a SQL query for c_name, city, s_id where c_id is between 3005 to 3009.
                                          23
                                                               Reg. No. – 11023210098
                                                                         Section – “B”
Date – 11th Nov 2024                                              Name – Aayush Mehta
Write a SQL query for c_name, city, s_id where c_id is not between 3005 to 3009.
Write a SQL query for customer table where c_name starts from ‘d’.
Write a SQL query for customer table where c_name ends with ‘h’.
Write a SQL query for customer who recites in city starts with ‘l’ followed by one wild
card character and then ‘nd’ and then two wild card characters.
                                           24
                                                                 Reg. No. – 11023210098
                                                                           Section – “B”
Date – 11th Nov 2024                                                   Name – Aayush Mehta
Post-Questions:
   1) Difference between Primary Key and Unique Constraint?
      Ans-
       Primary Key:                                 Unique Constraint:
       A primary key uniquely identifies each       A unique constraint also ensures that all
       row in a table.                              values in a column or a group of
                                                    columns are distinct (unique).
       It cannot contain NULL values.
                                                    However, unlike the primary key, a
       A table can only have one primary key,       column with a unique constraint can
       and it can consist of single or multiple     contain NULL values, but there can only
       columns (composite key).                     be one NULL value per column.
       The primary key automatically creates a      A table can have multiple unique
       unique index for faster retrieval of data.   constraints.
       The primary key uniquely identifies          The unique constraint allows NULL
       rows and does not allow NULL values.         values but ensures that all non-NULL
                                                    values are unique.
      Blank Space (' '): Is a valid character value, but it indicates an empty string or a
      space in a text field.
      In SQL:
                                              25
                                                                      Reg. No. – 11023210098
                                                                                Section – “B”
Date – 11th Nov 2024                                              Name – Aayush Mehta
      Comparisons with NULL return UNKNOWN. For example, NULL = NULL is not
      true, as NULL represents an unknown value.
Program-6
                                           26
                                                                 Reg. No. – 11023210098
                                                                           Section – “B”
Date – 11th Nov 2024                                                 Name – Aayush Mehta
Aim  To study the aggregate functions (sum, count, max, min, average) and execute the
queries using these commands.
Pre-Questions:
   1) Define Data Integrity?
      Ans- Data Integrity refers to the accuracy, consistency, and reliability of data
      throughout its lifecycle. It ensures that the data remains unaltered and reliable when
      stored, processed, or retrieved. Data integrity can be enforced by database constraints
      like primary keys, foreign keys, and unique keys.
Insertion of data:
Write a SQL query to calculate total purchasing amount of all the orders.
                                             27
                                                                    Reg. No. – 11023210098
                                                                              Section – “B”
Date – 11th Nov 2024                                            Name – Aayush Mehta
Write a SQL query to calculate average purchasing amount of all the orders.
                                          28
                                                               Reg. No. – 11023210098
                                                                         Section – “B”
Date – 11th Nov 2024                                           Name – Aayush Mehta
Write a SQL query to find the highest purchasing amount ordered by each customer,
return customer id and maximum purchasing amount.
Write a SQL query to find the highest purchasing amount on ‘2024-06-08’ sold by each
salesperson, return salesman id and maximum purchasing amount.
Write a SQL query to find the highest purchasing amount generated by each
salesperson filter the rows for salesperson id in the range 2000 to 2400.
                                         29
                                                              Reg. No. – 11023210098
                                                                        Section – “B”
Date – 11th Nov 2024                                                   Name – Aayush Mehta
Post-Questions:
   1) Differentiate between a View and Table in SQL?
      Ans- Table: A table is a structured collection of data that is stored physically in a
      database.
      View: A view is a virtual table based on the result of an SQL query. It does not store
      data physically but dynamically retrieves data from underlying tables whenever
      accessed.
                                              30
                                                                      Reg. No. – 11023210098
                                                                                Section – “B”
Date – 11th Nov 2024                                                  Name – Aayush Mehta
Program-7
Aim  To study the grouping commands (group by, order by) and execute the queries using
these commands.
Pre-Questions:
   1. What is an Index?
      Ans- An index is a database object that improves the speed of data retrieval
      operations on a database table. It functions like a lookup table, allowing the database
      engine to find rows more quickly without scanning the entire table.
   3. What is Trigger?
      Ans- A trigger is a special type of stored procedure that automatically runs in
      response to certain events on a particular table or view. Common events include
      INSERT, UPDATE, and DELETE operations. Triggers help enforce business rules
      and maintain data integrity.
                                             31
                                                                     Reg. No. – 11023210098
                                                                               Section – “B”
Date – 11th Nov 2024                                         Name – Aayush Mehta
Insertion of Data:
Write a SQL query to get movie name from table group by and order by movie name in
ascending order:
Write a SQL query to get movie name, sum of gross from table group by movie name
order by sum of gross in descending order:
                                          32
                                                            Reg. No. – 11023210098
                                                                      Section – “B”
Date – 11th Nov 2024                                             Name – Aayush Mehta
Write a SQL query to get sum of gross, movie name, city from table group by movie
name and city order by city in ascending order:
Write a SQL query to get city from table group by city order by sum of gross:
Write a SQL query to get city, country, average of gross from table group by city order
by country:
                                           33
                                                                Reg. No. – 11023210098
                                                                          Section – “B”
Date – 11th Nov 2024                                                   Name – Aayush Mehta
Post-Questions:
   1. What is primary use of normalization?
      Ans- The primary use of normalization is to reduce data redundancy and improve data
      integrity in a relational database. By organizing tables and their relationships properly,
      normalization helps ensure that data is stored logically and can be easily updated
      without inconsistencies.
                                              34
                                                                      Reg. No. – 11023210098
                                                                                Section – “B”
Date – 11th Nov 2024                                                  Name – Aayush Mehta
Program-8
Pre-Questions:
       1. What is denormalization?
          Ans- Denormalization is a database optimization technique where redundant data is
          added to improve query performance. It involves combining data from multiple
          normalized tables into a single table, reducing the number of joins needed for
          complex queries. This approach improves read performance but can increase
          redundancy and storage requirements.
Insertion of data:
                                               35
                                                                     Reg. No. – 11023210098
                                                                               Section – “B”
Date – 11th Nov 2024                                            Name – Aayush Mehta
Write an SQL query to give temporary name to column employee_id as emp_id and to
get first name:
Write an SQL Query to get first and last name of all the employees as f_name and
l_name whose salary is above 200000:
                                          36
                                                               Reg. No. – 11023210098
                                                                         Section – “B”
Date – 11th Nov 2024                                                 Name – Aayush Mehta
Write an SQL query to get employee_id, salary and complete address of all the
employees:
Write an SQL query to rename the table name from employee to emp_one:
Post-Questions:
   1. Define database replication?
      Ans- Database replication involves copying and maintaining database objects like
      tables or transactions across multiple databases. This ensures data consistency and
      availability, often for fault tolerance or load balancing. Common replication types
      include Master-Slave Replication, Master-Master Replication, and Log-Based
      Replication.
Program-9
Pre-questions:
   1. What is the purpose of using alias in joins?
      Ans- Aliases are used in SQL joins to give temporary names to tables or columns,
      making the query more readable and reducing ambiguity, especially when dealing
      with self-joins or queries involving multiple tables with similar column names.
       WHERE Clause: Used to filter rows after the join has been performed. It applies
       additional conditions to the result set.
Make 4 tables:
Author: id, name, birth year, death year
Book: id, author id, title, publish year, publish house
Adaption: book id, type, title, rating, release year
Book Review: book id, review, author
Insertion of data:
                                                38
                                                                       Reg. No. – 11023210098
                                                                                 Section – “B”
Date – 11th Nov 2024        Name – Aayush Mehta
                       39
                            Reg. No. – 11023210098
                                      Section – “B”
Date – 11th Nov 2024        Name – Aayush Mehta
                       40
                            Reg. No. – 11023210098
                                      Section – “B”
Date – 11th Nov 2024                                              Name – Aayush Mehta
                                           41
                                                                 Reg. No. – 11023210098
                                                                           Section – “B”
Date – 11th Nov 2024                                             Name – Aayush Mehta
Write an SQL query to select book.title, author.name from book left join author on
book.author_id = author.id union select book.title, author.name from book right join
author on book.author_id = author.id;
Post-questions:
   1. What are acid properties?
                                          42
                                                                Reg. No. – 11023210098
                                                                          Section – “B”
Date – 11th Nov 2024                                                Name – Aayush Mehta
      NATURAL JOIN: Combines tables based on columns with the same names and
      compatible data types, automatically performing an equi-join on those columns.
Program-10
Pre-Questions:
   1. Having clause works like where clause if it is not work with group by clause.
      True or False?
      Ans- True
                                            43
                                                                   Reg. No. – 11023210098
                                                                             Section – “B”
Date – 11th Nov 2024                                                  Name – Aayush Mehta
Insertion of data:
Post-Questions:
   1. What happens to NULL when you use Group by?
      Ans- When using GROUP BY, NULL values are treated as a distinct group. This
      means that all rows with NULL in the grouped column will be grouped together.
      Nested Subquery: A subquery that is independent of the outer query and is executed
      only once.
                                           45
                                                                 Reg. No. – 11023210098
                                                                           Section – “B”