0% found this document useful (0 votes)
16 views10 pages

Writing Questions

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)
16 views10 pages

Writing Questions

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
You are on page 1/ 10

Writing Questions

1. Create Database schema, Create tables, assign Primary key

• 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)
);

o Primary Key is used to uniquely identify each record in a


table (e.g., emp_id in the example).

2. Design E-R Diagram & create Normalized database on given data. Explain
Normalization

• E-R Diagram: It's a visual representation of the entities (tables)


and their relationships.
o Example: An Employee entity with attributes like emp_id,
emp_name, and emp_salary, and a Department entity
with attributes like dept_id and dept_name.
• Normalization: It's the process of organizing data to reduce
redundancy. It involves splitting tables into smaller, related tables.
o 1NF: All values in a column are atomic (single value, no
lists).
o 2NF: Remove partial dependencies (every non-key attribute
depends on the whole primary key).
o 3NF: Remove transitive dependencies (non-key attributes
depend only on primary keys).

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);

o UPDATE: Modifies existing data.


sql
Copy code
UPDATE students SET age = 21 WHERE student_id = 1;

o DELETE: Removes data.


sql
Copy code
DELETE FROM students WHERE student_id = 1;

5. Explain SQL Operators & Write four Queries using SQL operators

• Operators: Used for comparisons and logic.


o Comparison Operators: =, !=, <, >, <=, >=
o Logical Operators: AND, OR, NOT
o Example Queries:
sql
Copy code
SELECT * FROM students WHERE age >= 18; -- Greater
than or equal
SELECT * FROM students WHERE name LIKE 'John%'; --
Starts with 'John'
SELECT * FROM students WHERE age BETWEEN 18 AND 25; -
- Between two values
SELECT * FROM students WHERE age = 20 AND name =
'John'; -- AND operator

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

• Date & Time Functions:


sql
Copy code
SELECT NOW(); -- Current date and time
SELECT DATE_ADD(NOW(), INTERVAL 1 DAY); -- Add 1 day

• Aggregate Functions:
sql
Copy code
SELECT COUNT(*) FROM students; -- Count rows
SELECT AVG(salary) FROM employees; -- Average salary

7. Write Queries using SQL functions: Aggregate functions

• 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

8. Explain & Execute Queries using SELECT command with WHERE,


HAVING, GROUP BY, & ORDER BY clauses

• WHERE: Filters records before grouping.


sql
Copy code
SELECT * FROM students WHERE age > 18;
• HAVING: Filters groups after aggregation.
sql
Copy code
SELECT department, COUNT(*) FROM employees GROUP BY
department HAVING COUNT(*) > 5;

• GROUP BY: Groups rows with similar values.


sql
Copy code
SELECT department, COUNT(*) FROM employees GROUP BY
department;

• ORDER BY: Sorts the result.


sql
Copy code
SELECT * FROM employees ORDER BY salary DESC;

9. Explain Joins & Execute the queries based on Inner & Outer join

• Inner Join: Returns matching rows from both tables.


sql
Copy code
SELECT employees.name, departments.name
FROM employees
INNER JOIN departments ON employees.dept_id =
departments.dept_id;

• 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;

o RIGHT JOIN: Similar to LEFT JOIN but returns all rows


from the right table.

10. Explain view & Write & Execute Query for Implementation of Views

• View: A virtual table based on a query.


sql
Copy code
CREATE VIEW employee_view AS
SELECT name, salary FROM employees WHERE salary >
50000;

• Select from View:


sql
Copy code
SELECT * FROM employee_view;

11. Explain, Create & Execute Indexes, Sequences & Synonyms in SQL

• Index: Speeds up query retrieval.


sql
Copy code
CREATE INDEX idx_name ON employees(name);

• Sequence: Generates unique numbers.


sql
Copy code
CREATE SEQUENCE emp_seq START WITH 1 INCREMENT BY 1;
SELECT emp_seq.NEXTVAL FROM dual;

• Synonym: Alias for a database object.


sql
Copy code
CREATE SYNONYM emp FOR employees;

12. Explain PL/SQL & Write a PL/SQL program to display Fibonacci series

• PL/SQL: A block-structured language used in Oracle for


procedural programming.
• Fibonacci Program:
sql
Copy code
DECLARE
a INT := 0;
b INT := 1;
c INT;
BEGIN
FOR i IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE(a);
c := a + b;
a := b;
b := c;
END LOOP;
END;

13. Write a PL/SQL program based on Exception Handling (Pre-defined)

• 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;

14. Write a PL/SQL program based on Exception Handling (User-defined


exceptions)

• 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;

o REVOKE: Removes privileges.


sql
Copy code
REVOKE SELECT, INSERT ON employees FROM john;

You might also like