**Q**: What are the sublanguages in SQL?
**A**:
SQL has several sublanguages, each designed for different purposes:
- **DDL (Data Definition Language)**: Used for defining and modifying
database objects like tables, indexes, and views. Examples include
`CREATE`, `ALTER`, `DROP`.
- **DML (Data Manipulation Language)**: Used for managing data within
database objects. Examples include `SELECT`, `INSERT`, `UPDATE`,
`DELETE`.
- **DCL (Data Control Language)**: Used for controlling access to data
within the database. Examples include `GRANT`, `REVOKE`.
- **TCL (Transaction Control Language)**: Used for managing transactions
in the database. Examples include `COMMIT`, `ROLLBACK`, `SAVEPOINT`.
**Q**: What is the difference between `DELETE` and `TRUNCATE`?
A**
- `DELETE` removes rows one at a time and logs each deletion, allowing for
triggers to be fired. It can have a `WHERE` clause to delete specific rows.
- `TRUNCATE` removes all rows from a table without logging individual row
deletions. It resets the table but does not fire triggers.
**Q**: What are constraints and their types?
**A**:
- **Primary Key**: Ensures unique identification for each row.
- **Foreign Key**: Ensures referential integrity between tables.
- **Unique**: Ensures all values in a column are unique.
- **Not Null**: Ensures a column cannot have null values.
- **Check**: Ensures all values in a column satisfy a specific condition.
- **Default**: Assigns a default value to a column if no value is specified.
***composite primary key *constraint
**Q**: What is the difference between a Primary Key and a Foreign Key?
**Primary Key**: A column or set of columns that uniquely identifies each
row in a table. It cannot have null values.
- **Foreign Key**: A column or set of columns that create a relationship
between two tables. It references the primary key of another table.
**Q**: What is the difference between a Primary Key and a Unique Key?
-A**
**Primary Key**: Uniquely identifies each row in a table and cannot have null values.
**Unique Key**: Ensures all values in a column are unique but can have one null value.
**Q**: What is a join and how many types of joins are there?
A**
A join is a SQL operation that combines rows from two or more tables based
on related columns. Types of joins include:
inner join, outer join, left outer join (left join ), right outer join (right
join ),full outer join (full join), self-join , cross join,
Explain Inner Join, Left Outer Join, Right Outer Join, Full Outer Join, Cross Join, and
Self Join.
- **Inner Join**: Selects records with matching values in both tables.
- **Left Outer Join**: Selects all records from the left table and matched
records from the right table.
- **Right Outer Join**: Selects all records from the right table and matched
records from the left table.
- **Full Outer Join**: Selects records when there is a match in either table.
- **Cross Join**: Combines each row of the first table with each row of the
second table.
- **Self Join**: A table is joined with itself to query hierarchical data.
**Q**: Explain `GROUP BY` and `ORDER BY`.
-A**
*GROUP BY*: Groups rows that have the same values in specified columns into aggregated data.
*ORDER BY*: Sorts the result set of a query by specified columns.
**Q**: What is the difference between `WHERE` and `HAVING`?
- A**
**WHERE**: Filters rows before any groupings are made.
**HAVING**: Filters groups after `GROUP BY` clause.
**Q**: What is a view?
**A**
: A view is a virtual table based on the result-set of a SQL statement. It allows
users to encapsulate complex queries and simplifies data access.
**Q**: What is a stored procedure?
**A**:
A stored procedure is a prepared SQL code that you can save and reuse. It
can take parameters and perform complex database operations.
**Q**: What is the difference between `UNION` and `UNION ALL`?
A**
**UNION**: Combines the result sets of two queries and removes duplicates.
UNION ALL**: Combines the result sets of two queries including duplicates.
**Q**: What are functions in SQL Server?
1.AGGRIGATE FUNCTIONS : SUM,MIN,MAX,AVG,COUNT
2.SCALAR FUNCTIONS : DATE PART ,DATENAME ,DATE DIFF,DATE ADD
3.STRING FUNCTIONS :
LEFT,RIHT,TRIM,LTRIM,RTRIM,LENGTH,DATALENGTH,CHARINDEX,PATINDEX,CO
NCAT,COALESC
CONVERT,CAST,REPLACE,REPLICATE ,SUBSTRING
4.NUMERICAL FUNCTIONS :
5.RANK FUNCTIONS : ROW_NUMBER (),RANK ,DENSE_RANK
6.WINDOWS OR ANALYTICAL FUNCTIONS :
LEAD,LAG,FIRST_VALUE,LAST_VALUE,RUNNING TOTAL