Writing Questions
Writing Questions
• Create Database:
sql
Copy code
CREATE DATABASE my_database;
• Create Table:
sql
Copy code
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
emp_salary DECIMAL(10, 2)
);
2. Design E-R Diagram & create Normalized database on given data. Explain
Normalization
3. Create & execute DDL Commands using SQL, Applying Key Constraints &
Explain with example
• DDL Commands:
o CREATE: Creates a table, view, or database.
o ALTER: Modifies an existing object.
o DROP: Deletes a table or database.
o Example:
sql
Copy code
CREATE TABLE students (
student_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT CHECK (age >= 18)
);
o Constraints:
▪ PRIMARY KEY: Uniquely identifies each record.
▪ NOT NULL: Ensures the column cannot be empty.
▪ CHECK: Ensures the data meets a certain condition.
4. Create & Execute DML commands using SQL. Explain DML Commands
with example
• DML Commands:
o INSERT: Adds data to a table.
sql
Copy code
INSERT INTO students (student_id, name, age) VALUES
(1, 'John Doe', 20);
5. Explain SQL Operators & Write four Queries using SQL operators
6. Write Queries using SQL functions: String, Arithmetic, Date & Time,
Aggregate functions
• String Functions:
sql
Copy code
SELECT CONCAT(first_name, ' ', last_name) FROM
employees; -- Combine names
SELECT LENGTH(name) FROM students; -- Length of name
• Arithmetic Functions:
sql
Copy code
SELECT salary + 500 FROM employees; -- Add 500 to
salary
SELECT price * quantity FROM orders; -- Multiply
price by quantity
• Aggregate Functions:
sql
Copy code
SELECT COUNT(*) FROM students; -- Count rows
SELECT AVG(salary) FROM employees; -- Average salary
• Aggregate Functions:
sql
Copy code
SELECT SUM(salary) FROM employees; -- Total salary
SELECT MAX(salary) FROM employees; -- Highest salary
SELECT MIN(salary) FROM employees; -- Lowest salary
SELECT AVG(salary) FROM employees; -- Average salary
9. Explain Joins & Execute the queries based on Inner & Outer join
• Outer Join: Returns all rows, with NULL for unmatched rows.
o LEFT JOIN:
sql
Copy code
SELECT employees.name, departments.name
FROM employees
LEFT JOIN departments ON employees.dept_id =
departments.dept_id;
10. Explain view & Write & Execute Query for Implementation of Views
11. Explain, Create & Execute Indexes, Sequences & Synonyms in SQL
12. Explain PL/SQL & Write a PL/SQL program to display Fibonacci series
• Pre-defined Exception:
sql
Copy code
DECLARE
num INT := 0;
BEGIN
IF num = 0 THEN
RAISE ZERO_DIVIDE;
END IF;
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('Cannot divide by zero!');
END;
• User-defined Exception:
sql
Copy code
DECLARE
age INT := 15;
age_exception EXCEPTION;
BEGIN
IF age < 18 THEN
RAISE age_exception;
END IF;
EXCEPTION
WHEN age_exception THEN
DBMS_OUTPUT.PUT_LINE('Age must be 18 or above');
END;
15. Explain & Execute DCL commands using SQL, Create Users, Grant,
Revoke Privileges to users
• DCL Commands:
o GRANT: Gives privileges to a user.
sql
Copy code
GRANT SELECT, INSERT ON employees TO john;