UNIT – IV: Structured Query Language (SQL)
1. Concept of SQL
-
SQL is a standard language for managing relational databases.
-
Categories: DDL, DML, DCL, TCL.
2. Data Types in SQL
-
Integer, Float, Double → Numeric values
-
Char(n), Varchar(n) → Strings
-
Date, Time, Timestamp → Date & time values
-
Number(p,s) → Numeric with precision
3. DDL Commands
-
CREATE: Creates table.
-
ALTER: Modifies table structure.
-
DROP: Deletes table.
-
RENAME: Changes table name.
-
TRUNCATE: Deletes all rows, keeps structure.
-
COMMENT: Adds comments.
4. DML Commands
-
SELECT: Retrieve data.
-
INSERT: Add rows.
-
UPDATE: Modify rows.
-
DELETE: Remove rows.
-
MERGE: Insert/update simultaneously.
-
CALL: Call stored procedure.
-
EXPLAIN PLAN: Shows execution plan.
-
LOCK TABLE: Prevents changes by others.
5. DCL Commands
-
GRANT: Give access.
-
REVOKE: Remove access.
6. TCL Commands
-
COMMIT: Save changes.
-
ROLLBACK: Undo changes.
-
SAVEPOINT: Create checkpoint.
-
SET TRANSACTION: Modify transaction properties.
7. WHERE Clause with Operators
-
Comparison (=, >, <, !=)
-
Logical (AND, OR, NOT)
-
LIKE (pattern matching)
-
IN, BETWEEN
8. Constraints
-
Primary Key
-
Foreign Key
-
Unique
-
Check
-
Not Null
9. SQL Functions
-
Aggregate: COUNT, SUM, AVG, MAX, MIN.
-
Scalar: UPPER, LOWER, LENGTH, SUBSTR, ROUND.
10. SQL Joins
-
INNER JOIN: Common records.
-
LEFT JOIN: All left + matching right.
-
RIGHT JOIN: All right + matching left.
-
FULL OUTER JOIN: All records from both.
Unit–IV Long Questions with Answers
-
Explain SQL and its uses in DBMS.
-
Explain SQL data types with examples.
-
Explain CREATE, ALTER, DROP with examples.
-
Differentiate DELETE and TRUNCATE.
-
Explain SELECT with WHERE clause.
-
Explain INSERT with example.
-
Explain UPDATE with example.
-
Explain DELETE with example.
-
Explain MERGE with example.
-
Explain GRANT and REVOKE with syntax.
-
Explain COMMIT, ROLLBACK, SAVEPOINT.
-
Explain constraints with examples.
-
Explain aggregate functions with SQL queries.
-
Explain scalar functions with SQL queries.
-
Explain SQL joins with examples.
Q1. Define SQL and explain its categories.
Answer:
SQL is a structured query language used to interact with relational databases.
Categories:
-
DDL – Create structure
-
DML – Manipulate data
-
DCL – Control permissions
-
TCL – Manage transactions
Q2. Explain SQL data types with examples.
Answer:
-
Integer: INT, SMALLINT
-
Float: REAL, FLOAT
-
Char: CHAR(10), VARCHAR(20)
-
Date/Time: DATE, TIME, TIMESTAMP
Example:
Q3. Explain CREATE, ALTER, DROP with examples.
Answer:
-
CREATE:
CREATE TABLE Student(RollNo INT, Name VARCHAR(20));
-
ALTER:
ALTER TABLE Student ADD Age INT;
-
DROP:
DROP TABLE Student;
Q4. Differentiate DELETE and TRUNCATE with example.
Answer:
-
DELETE: Removes rows, can use WHERE. Rollback possible.
-
TRUNCATE: Removes all rows, faster, cannot rollback.
Q5. Explain SELECT with WHERE clause.
Answer:
Q6. Explain INSERT with example.
Answer:
Q7. Explain UPDATE with example.
Answer:
Q8. Explain DELETE with example.
Answer:
Q9. Explain MERGE with example.
Answer:
Q10. Explain GRANT and REVOKE.
Answer:
-
GRANT: Gives permission
-
REVOKE: Removes permission
Q11. Explain COMMIT, ROLLBACK, SAVEPOINT.
Answer:
-
COMMIT: Saves permanently.
-
ROLLBACK: Undo changes.
-
SAVEPOINT: Marks intermediate point.
Q12. Explain constraints in SQL.
Answer:
-
Primary Key
-
Foreign Key
-
Unique
-
Not Null
-
Check
Example:
Q13. Explain aggregate functions with examples.
Answer:
Q14. Explain scalar functions with examples.
Answer:
Q15. Explain SQL joins with examples.
Answer:
-
INNER JOIN: Only matching rows
-
LEFT JOIN: All left + matching right
-
RIGHT JOIN: All right + matching left
-
FULL JOIN: All records
Comments
Post a Comment