0% found this document useful (0 votes)
28 views28 pages

SQL Queries for Database Management

Uploaded by

akshad103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views28 pages

SQL Queries for Database Management

Uploaded by

akshad103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Query 1.

Write SQL query for Data Definition and Data


Manipulation Language.

Sol. 1

DDL commands :

1. Create Database (DDL)


The first step is to create the database, where we will store our table. We will
create a database called StudentDB.

Next, you need to switch to the newly created database, so that you can create
tables and insert data into it.

2. Create Table (DDL)

After creating the database, the next step is to define the structure of the table.
We will create the Student table with columns such as student_id,
student_name, age, gender, and grade.
DDL commands :

3. Insert Data (DML)

After creating the table, we will insert 10 records into the Student table.

4. Select Data (DML)

To check if the data has been successfully inserted, you can use the SELECT query
to retrieve all the records from the Student table.
5. Update Data (DML)

If you want to update any data (e.g., changing Ali's grade from 'C' to 'B'), you can
use the UPDATE statement.

6. Delete Data (DML)

If you need to delete a record, such as removing Ethan from the table, use the
DELETE statement.

7. Drop Table (DDL)

If you want to delete the table structure entirely (and remove all data), you can
use the DROP statement.
8. Drop Database (DDL)

Finally, if you want to delete the entire database, including all tables and data
within it, you can use the DROP DATABASE statement.
Query 2. Write SQL queries using logical operators.

Sol 2.

Logical operators in SQL allow you to combine multiple conditions in a WHERE


clause, enabling more complex filtering of data

1. AND Operator

The AND operator combines two or more conditions, and the result is true only
if all conditions are true.

2. NOT Operator
The NOT operator reverses the result of a condition. If the condition is true, NOT
will make it false, and if the condition is false, NOT will make it true.

2. OR Operator

The OR operator combines two or more conditions, and the result is true if at
least one condition is true.
4. IS NULL / IS NOT NULL Operator

The IS NULL operator checks for NULL values in a column. The IS NOT NULL
operator checks for non-NULL values.

5. LIKE Operator

The LIKE operator is used for pattern matching with wildcard characters. The
most common wildcards are:
• %: Matches zero or more characters.
• _: Matches exactly one character.
7. IN Operator

The IN operator allows you to specify multiple values in a WHERE clause. It is


shorthand.

8. BETWEEN Operator

The BETWEEN operator is used to filter the result set within a specific range. It
is inclusive of the start and end values.
Ques 3. Write SQL queries using SQL operators .

Sol 3.

SQL operators are used to perform operations on data in the database.

1. Comparison Operators

a. = or Equals to .

(Students who have grade A)

b. != or <> (Not equal to)

(Student who didn’t get grade A)


b. < or Lesser than

(Find students who are younger than 22 years.)

d. > (Greater than)

(Find students who have more than 80 total marks.)


e. >= (Greater than or equal to)

(Find students who are older than or equal to 22 years.)


Ques 4. Write SQL query using character , number , date and group.

Sol 4.

1. Character Function (e.g CONCAT)

(Concatenate the student's name and gender into a single string.)

2. Number (e.g. Using ROUND to round a number)

(Round the total_marks to 2 decimal places.)

3. Date (e.g. Using CURRENT_DATE to get today's date)

(Find the current date from the database.)


4. Group (e.g Grouping functions are used with GROUP BY to aggregate data
based on certain criteria.)

(Find how many students are in each grade group.)


Ques 5. Write SQL query for relational algebra.

Sol 5.

1. Selection (σ):

Relational Algebra:
σ condition (Table)

SQL Query :

(This SQL query selects all rows (students) where the grade is 'A' from the
Student table)

2. Projection (π):

Relational Algebra:

Π column1,column2 (Table)

SQL Query :
3. Union (∪) :

Relational Algebra:
Table1 ∪ Table2

SQL Equivalent:

4. Set Difference (−):

Relational Algebra:
Table1 - Table2

SQL Equivalent:
5. Cartesian Product (×):

Relational Algebra:
Table1×Table2

SQL Equivalent :

(The CROSS JOIN generates all possible combinations of rows from the
Teacher and Department tables.)

6. Join (⨝):

Relational Algebra:
Table1 ⋈ Table2

Equivalent SQL Query :


(This SQL query joins the Student and Course tables based on the
student_id)

7. Natural Join (⨝):

Relational Algebra:
Table1 ⋈ Table2 (Natural join)

SQL Equivalent:

(This SQL query performs a natural join on the Student and Course tables,
automatically using the common column(s) (e.g., student_id).)
8. Renaming (ρ):

Relational Algebra:
ρ X (Table)

SQL Equivalent:

(This SQL query renames the columns student_name to name and


total_marks to marks in the result.)

9. Intersection (∩):

Relational Algebra:
Table1 ∩ Table2
SQL Equivalent:

(This SQL query returns the common students who have both grades 'A' and
'B'. (Note: This will usually return an empty result, as no student can have
two grades at the same time.)
Ques 6. Write SQL queries for extracting data from more than one
table.

Sol 6.

a.) Using Inner Join on multiple tables

b.) Using Left Join


Ques 7. Write SQL queries for sub queries and nested queries.

Sol 7.

a.) Subquery to get the number of courses each student is


enrolled in:
b.) Nested Query with Aggregate Functions :
Ques 8. Write a program for PL/SQL.

Sol :

Showing connectivity and a procedural SQL program to add 2 no.


Ques 9. Concept of ROLL BACK , COMMIT AND CHECK POINTS.

Sol :

COMMIT: This will save the changes to the database permanently.

ROLLBACK: This will undo all changes made since the last commit (or since
the start if no commit was made).

CHECKPOINT: This refers to committing the transaction at specific intervals


or after certain operations. It’s useful for large batches to avoid losing all
progress if an error occurs.
Code –

Execution of code on notepad using sql plus command client –


Ques 10. Create Views , Cursors and Triggers.

Sol :

- View: active_accounts filters active accounts.

- Cursor: Loops through all accounts and outputs their details.

- Trigger: Logs changes to the account balance into the


account_balance_log table.

Code in notepad file (to be executed on sql plus server):


SQL> @acc --- file name (the output generated by this command)

You might also like