DBMS Exam Preparation
DBMS Exam Preparation
4. Write a SQL query to retrieve the sale_id and total_price from the
Sales table for sales made on January 3, 2024.
Explanation:
- We need to filter records from the `Sales` table where the `sale_date`
is January 3, 2024.
- The query will use the `SELECT` statement with a `WHERE` clause
to filter the date.
Exam Answer:
```sql
CREATE DATABASE SalesDB;
USE SalesDB;
Explanation:
- SQL supports aggregate functions to perform calculations on
multiple rows.
- Common aggregate operators:
1. **SUM()** -> Adds values.
2. **AVG()** -> Calculates average.
3. **COUNT()** -> Counts rows.
4. **MAX()** -> Finds maximum value.
5. **MIN()** -> Finds minimum value.
Exam Answer:
```sql
SELECT COUNT(*) AS total_sales, SUM(total_price) AS total_revenue
FROM Sales;
```
------------------------------------
Explanation:
- A **view** is a virtual table based on the result of an SQL query.
- It is used to simplify complex queries and enhance security.
Exam Answer:
```sql
CREATE VIEW SalesSummary AS
SELECT Sale_id, product_id, total_price FROM Sales;
Explanation:
- Joins combine rows from two or more tables based on a related
column.
- Types of Joins:
1. **INNER JOIN** -> Matches rows in both tables.
2. **LEFT JOIN** -> All rows from the left table + matching rows from
the right table.
3. **RIGHT JOIN** -> All rows from the right table + matching rows
from the left table.
4. **FULL JOIN** -> All rows from both tables.
Exam Answer:
```sql
SELECT Sales.Sale_id, Sales.total_price, Salesman.name
FROM Sales
INNER JOIN Salesman ON Salesman.salesman_id = Sales.Sale_id;
```
------------------------------------
Explanation:
- **Numeric Types:** `INT`, `DECIMAL`, `FLOAT`
- **String Types:** `VARCHAR`, `TEXT`
- **Date/Time Types:** `DATE`, `DATETIME`
- **Boolean Type:** `BOOLEAN`
------------------------------------
Explanation:
- **DDL (Data Definition Language):** Defines structure (`CREATE`,
`ALTER`, `DROP`).
- **DML (Data Manipulation Language):** Modifies data (`INSERT`,
`UPDATE`, `DELETE`).
Exam Answer:
```sql
-- DDL Example
CREATE TABLE Customers (
customer_id INT PRIMARY KEY,
name VARCHAR(100)
);
-- DML Example
INSERT INTO Customers VALUES (1, 'Alice');
UPDATE Customers SET name = 'Bob' WHERE customer_id = 1;
DELETE FROM Customers WHERE customer_id = 1;
```
------------------------------------
Explanation:
- Conflict Serializability ensures that a given schedule can be
transformed into a serial schedule by swapping non-conflicting
operations.
- Two operations conflict if:
1. They belong to different transactions.
2. They access the same data.
3. At least one of them is a write operation.
Exam Answer:
A schedule is **Conflict Serializable** if it can be transformed into a
serial schedule by swapping non-conflicting operations.
Example:
Consider two transactions, **T1** and **T2**:
Since `T1` and `T2` both **read and write A**, a **conflict** exists. By
drawing a **precedence graph**, if a cycle is found, the schedule is
**not conflict serializable**.
------------------------------------