Skip to content

Commit 0a3b8e8

Browse files
authored
Merge pull request #6 from rahull0328/dev
added new answers
2 parents b75606e + 8fbd508 commit 0a3b8e8

File tree

1 file changed

+296
-0
lines changed

1 file changed

+296
-0
lines changed

README.md

Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,302 @@ JOIN TableB
10361036
ON TableA.id = TableB.id;
10371037
```
10381038
1039+
<div align="right">
1040+
<b><a href="#table-of-contents">↥ back to top</a></b>
1041+
</div>
1042+
1043+
## # 13. SQL RegEx
1044+
1045+
<br/>
1046+
1047+
## Q. How to use REGEXP in SQL Query?
1048+
1049+
<div align="right">
1050+
<b><a href="#table-of-contents">↥ back to top</a></b>
1051+
</div>
1052+
1053+
## # 14. SQL Indexes
1054+
1055+
<br/>
1056+
1057+
## Q. What are indexes in a Database?
1058+
1059+
Indexing is a way to optimize the performance of a database by minimizing the number of disk accesses required when a query is processed. It is a data structure technique which is used to quickly locate and access the data in a database.
1060+
1061+
Indexes are created using a few database columns
1062+
1063+
* The first column is the **Search key** that contains a copy of the primary key or candidate key of the table. These values are stored in sorted order so that the corresponding data can be accessed quickly.
1064+
1065+
* The second column is the **Data Reference** or **Pointer** which contains a set of pointers holding the address of the disk block where that particular key value can be found.
1066+
1067+
|Sl.No|Query | Description |
1068+
|-----|------------------------------------|---------------------------------------------------|
1069+
| 01. |CREATE INDEX index_name ON t(c1, c2) |Create an index on columns c1 and c2 of the table t |
1070+
| 02. |CREATE UNIQUE INDEX index_name ON t(c3, c4) |Create a unique index on columns c3 and c4 of the table t |
1071+
| 03. |DROP INDEX index_name |Drop an index |
1072+
1073+
**Example:**
1074+
1075+
```sql
1076+
-- Create Index
1077+
CREATE INDEX <index_name> ON <table_name> (column1, column2, ...)
1078+
1079+
-- Show Index
1080+
SHOW INDEX FROM <table_name>;
1081+
1082+
-- Alter Index
1083+
ALTER TABLE <table_name> ADD INDEX(`column_name`);
1084+
1085+
-- Drop Index
1086+
DROP INDEX index_name ON <table_name>;
1087+
```
1088+
1089+
<div align="right">
1090+
<b><a href="#table-of-contents">↥ back to top</a></b>
1091+
</div>
1092+
1093+
## Q. What is an index represent in relational database model?
1094+
1095+
Index is a way to provide quick access to the data and structure. It has indexes maintain and can be created to combine attributes on a relation. Index allows the queries to filter out the searches faster and matching data can be found earlier with simplicity.
1096+
1097+
For example: It is same as the book where by using the index you can directly jump to a defined section. In relational database there is a provision to give multiple indexing techniques to optimize the data distribution.
1098+
1099+
<div align="right">
1100+
<b><a href="#table-of-contents">↥ back to top</a></b>
1101+
</div>
1102+
1103+
#### Q. What is the difference between Cluster and Non-Cluster Index?
1104+
#### Q. How to create index in SQL Server?
1105+
1106+
<div align="right">
1107+
<b><a href="#table-of-contents">↥ back to top</a></b>
1108+
</div>
1109+
1110+
## Q. What are the types of indexes in sql?
1111+
1112+
**1. Clustered Index:**
1113+
1114+
Clustered index is the type of indexing that establishes a physical sorting order of rows. Clustered index is like Dictionary; in the dictionary, sorting order is alphabetical and there is no separate index page.
1115+
1116+
**2. Non-clustered:**
1117+
1118+
Non-Clustered index is an index structure separate from the data stored in a table that reorders one or more selected columns. The non-clustered index is created to improve the performance of frequently used queries not covered by a clustered index. It\'s like a textbook; the index page is created separately at the beginning of that book.
1119+
1120+
<div align="right">
1121+
<b><a href="#table-of-contents">↥ back to top</a></b>
1122+
</div>
1123+
1124+
## # 15. SQL Wildcards
1125+
1126+
<br/>
1127+
1128+
## Q. What are the ways to use wildcards in sql?
1129+
1130+
<div align="right">
1131+
<b><a href="#table-of-contents">↥ back to top</a></b>
1132+
</div>
1133+
1134+
## # 16. SQL Date Format
1135+
1136+
<br/>
1137+
1138+
## Q. What is difference between timestamp and datetime in SQL?
1139+
1140+
*ToDo*
1141+
1142+
<div align="right">
1143+
<b><a href="#table-of-contents">↥ back to top</a></b>
1144+
</div>
1145+
1146+
## # 17. SQL Transactions
1147+
1148+
<br/>
1149+
1150+
## Q. What is transactions in SQL?
1151+
1152+
A SQL transaction is a grouping of one or more SQL statements that interact with a database. A transaction in its entirety can commit to a database as a single logical unit or rollback (become undone) as a single logical unit.
1153+
1154+
In SQL, transactions are essential for maintaining database integrity. They are used to preserve integrity when multiple related operations are executed concurrently, or when multiple users interact with a database concurrently.
1155+
1156+
**Properties of Transactions:**
1157+
1158+
Transactions have the following four standard properties, usually referred to by the acronym ACID.
1159+
1160+
* **Atomicity** − ensures that all operations within the work unit are completed successfully. Otherwise, the transaction is aborted at the point of failure and all the previous operations are rolled back to their former state.
1161+
1162+
* **Consistency** − ensures that the database properly changes states upon a successfully committed transaction.
1163+
1164+
* **Isolation** − enables transactions to operate independently of and transparent to each other.
1165+
1166+
* **Durability** − ensures that the result or effect of a committed transaction persists in case of a system failure.
1167+
1168+
**Transaction Control:**
1169+
1170+
The following commands are used to control transactions.
1171+
1172+
* **COMMIT** − to save the changes.
1173+
1174+
* **ROLLBACK** − to roll back the changes.
1175+
1176+
* **SAVEPOINT** − creates points within the groups of transactions in which to ROLLBACK.
1177+
1178+
* **SET TRANSACTION** − Places a name on a transaction.
1179+
1180+
**Example:**
1181+
1182+
```sql
1183+
-- Exmaple - 01
1184+
1185+
CREATE TABLE widgetInventory (
1186+
id SERIAL,
1187+
description VARCHAR(255),
1188+
onhand INTEGER NOT NULL
1189+
);
1190+
1191+
CREATE TABLE widgetSales (
1192+
id SERIAL,
1193+
inv_id INTEGER,
1194+
quan INTEGER,
1195+
price INTEGER
1196+
);
1197+
1198+
INSERT INTO widgetInventory ( description, onhand ) VALUES ( 'rock', 25 );
1199+
INSERT INTO widgetInventory ( description, onhand ) VALUES ( 'paper', 25 );
1200+
INSERT INTO widgetInventory ( description, onhand ) VALUES ( 'scissors', 25 );
1201+
1202+
1203+
START TRANSACTION;
1204+
INSERT INTO widgetSales ( inv_id, quan, price ) VALUES ( 1, 5, 500 );
1205+
UPDATE widgetInventory SET onhand = ( onhand - 5 ) WHERE id = 1;
1206+
COMMIT;
1207+
1208+
SELECT * FROM widgetInventory;
1209+
SELECT * FROM widgetSales;
1210+
1211+
START TRANSACTION;
1212+
INSERT INTO widgetInventory ( description, onhand ) VALUES ( 'toy', 25 );
1213+
ROLLBACK;
1214+
SELECT * FROM widgetInventory;
1215+
SELECT * FROM widgetSales;
1216+
```
1217+
1218+
<div align="right">
1219+
<b><a href="#table-of-contents">↥ back to top</a></b>
1220+
</div>
1221+
1222+
## Q. What is the purpose of acid properties?
1223+
1224+
* ACID stands for Atomicity, Consistency, Isolation and durability and it plays an important role in the database.
1225+
1226+
* These properties allow the database to be more convenient to access and use. This allows data to be shared more safely in between the tables.
1227+
1228+
* If these properties are not being implemented then the data will become inconsistent and inaccurate.
1229+
1230+
* It helps in maintaining the accuracy of the data in the database.
1231+
1232+
<div align="right">
1233+
<b><a href="#table-of-contents">↥ back to top</a></b>
1234+
</div>
1235+
1236+
## Q. What is a NOLOCK?
1237+
1238+
* NOLOCK is used to improve concurrency on a busy system.
1239+
1240+
* On data read, no lock can be taken on SELECT statement.
1241+
1242+
* When some other process is updating the data on the same time you are reading it is known as dirty read.
1243+
1244+
* Read (Shared) locks are taken by SELECT Statements.
1245+
1246+
* Simultaneous access of multiple SELECT statements is allowed in Shared lock but modification process is not allowed.
1247+
1248+
* The result to your system is blocking.
1249+
1250+
* Update will start on completion of all the reads.
1251+
1252+
<div align="right">
1253+
<b><a href="#table-of-contents">↥ back to top</a></b>
1254+
</div>
1255+
1256+
## Q. What is a WITH(NOLOCK)?
1257+
1258+
- WITH(NOLOCK) is used to unlock the data which is locked by the transaction that is not yet committed. This command is used before SELECT statement.
1259+
- When the transaction is committed or rolled back then there is no need to use NOLOCK function because the data is already released by the committed transaction.
1260+
- Syntax: WITH(NOLOCK)
1261+
1262+
**Example:**:
1263+
1264+
```sql
1265+
SELECT * FROM EmpDetails WITH(NOLOCK)
1266+
WITH(NOLCOK) is similar as READ UNCOMMITTED.
1267+
```
1268+
1269+
<div align="right">
1270+
<b><a href="#table-of-contents">↥ back to top</a></b>
1271+
</div>
1272+
1273+
#### Q. What are different transaction levels in SQL?
1274+
#### Q. What are the different locks in SQL?
1275+
1276+
*ToDo*
1277+
1278+
<div align="right">
1279+
<b><a href="#table-of-contents">↥ back to top</a></b>
1280+
</div>
1281+
1282+
## # 18. SQL Functions
1283+
1284+
<br/>
1285+
1286+
#### Q. What is a function in SQL Server?
1287+
#### Q. What are the different types of functions in SQL Server?
1288+
## Q. What are the reporting aggregate functions available in SQL?
1289+
1290+
In database management, an aggregate function is a function where the values of multiples rows are grouped to form a single value.
1291+
1292+
|Sl.No |Function | Description |
1293+
|------|-----------|---------------------------------------------------|
1294+
| 01. |COUNT |Return the number of rows in a certain table/view |
1295+
| 02. |SUM |Accumulate the values|
1296+
| 03. |AVG |Returns the average for a group of values|
1297+
| 04. |MIN |Returns the smallest value of the group|
1298+
| 05. |MAX |Returns the largest value of the group|
1299+
1300+
#### Q. What are aggregate and scalar functions?
1301+
#### Q. What are all the Common SQL Function?
1302+
1303+
<div align="right">
1304+
<b><a href="#table-of-contents">↥ back to top</a></b>
1305+
</div>
1306+
1307+
## # 19. SQL View
1308+
1309+
<br/>
1310+
1311+
## Q. What is View in SQL?
1312+
1313+
A view is a virtual table that is a result of a query. They can be extremely useful and are often used as a security mechanism, letting users access the data through the view, rather than letting them access the underlying base table:
1314+
1315+
**Syntax:**
1316+
1317+
```sql
1318+
CREATE VIEW view_name AS
1319+
SELECT column1, column2, ...
1320+
FROM table_name
1321+
WHERE condition;
1322+
```
1323+
1324+
**Example:**
1325+
1326+
```sql
1327+
--- Creating a View
1328+
1329+
CREATE VIEW trackView AS
1330+
SELECT id, album_id, title, track_number, duration DIV 60 AS m, duration MOD 60 AS s
1331+
FROM track;
1332+
SELECT * FROM trackView;
1333+
```
1334+
10391335
<div align="right">
10401336
<b><a href="#table-of-contents">↥ back to top</a></b>
10411337
</div>

0 commit comments

Comments
 (0)