Docsity

SQL Basics: Key Concepts and Commands, Study notes of Computer Programming

Toggle sidebar
bg1
Basics of SQL
Q1. What is SQL?
SQL (Structured Query Language) is a standard language used to store, manage, and retrieve data
from relaonal databases.
Q2. What is a Database?
A database is an organized collecon of data stored and accessed electronically.
Q3. What is a Table?
A table is a collecon of related data organized in rows and columns in a database.
Q4. What is a Primary Key?
A primary key is a column (or set of columns) that uniquely idenfies each row in a table.
Example: EMP_ID in an employee table.
Q5. What is a Foreign Key?
A foreign key is a column in one table that links to the primary key of another table to maintain
referenal integrity.
SQL Commands
Q6. Difference between DDL, DML, DCL, and TCL?
DDL (Data Definion Language): Defines structure → CREATE, ALTER, DROP, TRUNCATE
DML (Data Manipulaon Language): Manages data → SELECT, INSERT, UPDATE, DELETE
DCL (Data Control Language): Manages permissions → GRANT, REVOKE
TCL (Transacon Control Language): Manages transacons → COMMIT, ROLLBACK,
SAVEPOINT
Q7. What is the difference between DELETE, TRUNCATE, and DROP?
DELETE: Removes specific rows, can use WHERE, rollback possible
TRUNCATE: Removes all rows, no WHERE, resets identy, cannot rollback
DROP: Removes the enre table (data + structure), cannot rollback
Constraints
Q8. What is a Constraint in SQL?
A constraint is a rule applied on a column to ensure data integrity.
Examples: PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, NOT NULL, DEFAULT
Data Organization
Q9. What is Normalizaon?
Normalizaon is the process of organizing data to reduce redundancy and improve data integrity.
Example: Spling customer details into separate tables like Customers, Orders, and Payments.
Q10. What is a View in SQL?
A view is a virtual table based on a SQL query that displays data from one or more tables without
storing it physically.
pf3
pf4

Partial preview of the text

Download SQL Basics: Key Concepts and Commands and more Study notes Computer Programming in PDF only on Docsity!

Basics of SQL

Q1. What is SQL? SQL (Structured Query Language) is a standard language used to store, manage, and retrieve data from relaƟonal databases. Q2. What is a Database? A database is an organized collecƟon of data stored and accessed electronically. Q3. What is a Table? A table is a collecƟon of related data organized in rows and columns in a database. Q4. What is a Primary Key? A primary key is a column (or set of columns) that uniquely idenƟfies each row in a table. Example: EMP_ID in an employee table. Q5. What is a Foreign Key? A foreign key is a column in one table that links to the primary key of another table to maintain referenƟal integrity.

SQL Commands

Q6. Difference between DDL, DML, DCL, and TCL?  DDL (Data DefiniƟon Language): Defines structure → CREATE, ALTER, DROP, TRUNCATE  DML (Data ManipulaƟon Language): Manages data → SELECT, INSERT, UPDATE, DELETE  DCL (Data Control Language): Manages permissions → GRANT, REVOKE  TCL (TransacƟon Control Language): Manages transacƟons → COMMIT, ROLLBACK, SAVEPOINT Q7. What is the difference between DELETE, TRUNCATE, and DROP?  DELETE: Removes specific rows, can use WHERE, rollback possible  TRUNCATE: Removes all rows, no WHERE, resets idenƟty, cannot rollback  DROP: Removes the enƟre table (data + structure), cannot rollback

Constraints

Q8. What is a Constraint in SQL? A constraint is a rule applied on a column to ensure data integrity. Examples: PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, NOT NULL, DEFAULT

Data Organization

Q9. What is NormalizaƟon? NormalizaƟon is the process of organizing data to reduce redundancy and improve data integrity. Example: Spliƫng customer details into separate tables like Customers, Orders, and Payments. Q10. What is a View in SQL? A view is a virtual table based on a SQL query that displays data from one or more tables without storing it physically.

Q11. What is the difference between CHAR and VARCHAR?  CHAR(n): Fixed-length, always uses n characters (pads with spaces).  VARCHAR(n): Variable-length, uses only needed characters. Q12. What is the difference between HAVING and WHERE?  WHERE: Filters rows before grouping.  HAVING: Filters groups aŌer GROUP BY.

Joins

Q13. What are Joins in SQL? Joins combine rows from two or more tables based on a related column.  INNER JOIN: Returns matching rows only  LEFT JOIN: Returns all rows from leŌ table + matched rows  RIGHT JOIN: Returns all rows from right table + matched rows  FULL JOIN: Returns all rows from both tables  SELF JOIN: Table joined with itself  CROSS JOIN: Returns Cartesian product of rows

Subqueries and Functions

Q14. What is a Subquery? A subquery is a query inside another SQL query, usually enclosed in parentheses. Q15. What are Aggregate FuncƟons in SQL? FuncƟons that return a single value for a group of rows: COUNT(), SUM(), AVG(), MAX(), MIN() Q16. What are Scalar FuncƟons in SQL? FuncƟons that return a single value for each row: LEN(), UPPER(), LOWER(), ROUND(), GETDATE()

Window Functions

Q25. What is the difference between RANK(), DENSE_RANK(), and ROW_NUMBER()?  RANK(): Skips ranks if Ɵes.  DENSE_RANK(): No skipping of ranks.  ROW_NUMBER(): Unique sequence numbers without considering Ɵes. Q26. What is a CTE (Common Table Expression)? A CTE is a temporary result set used within a query.

Special Joins

Q27. What is Self Join? A join where a table is joined with itself. Q28. What is a Cartesian Join (Cross Join)? It returns the product of all rows from two tables. Example: 5 rows × 4 rows = 20 results.

Functions & Identity

Q29. What are Aggregate FuncƟons in SQL? COUNT(), SUM(), AVG(), MAX(), MIN() Q30. What is the difference between DELETE and TRUNCATE on idenƟty column?  DELETE: Does not reset idenƟty values.  TRUNCATE: Resets idenƟty to seed value.