You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| 01. |INSERT INTO t(column_list) VALUES(value_list)|Insert one row into a table named t|
830
+
| 02. |INSERT INTO t(column_list) VALUES (value_list), (value_list), … |Insert multiple rows into a table named t|
831
+
| 03. |INSERT INTO t1(column_list) SELECT column_list FROM t2 |Insert rows from t2 into a table named t1|
832
+
| 04. |UPDATE tSET c1 = new_value |Update a new value in table t in the column c1 for all rows|
833
+
| 05. |UPDATE tSET c1 = new_value, c2 = new_value WHERE condition|Updatevaluesin column c1 and c2 in table t that match the condition|
834
+
| 06. |DELETEFROM t |Delete all the rows from a table named t|
835
+
| 07. |DELETEFROM tWHERE condition |Delete all rows from that a table named t that match a certain condition|
836
+
837
+
<div align="right">
838
+
<b><a href="#table-of-contents">↥ back to top</a></b>
839
+
</div>
840
+
841
+
## # 10. SQL Delete
842
+
843
+
<br/>
844
+
845
+
## Q. What is difference between Truncate and Delete in SQL?
846
+
847
+
* TRUNCATE is a DDL (data definition language) command whereas DELETE is a DML (data manipulation language) command.
848
+
* We can'\t execute a trigger with TRUNCATE whereas with DELETE command, a trigger can be executed.
849
+
* We can use any condition in WHERE clause using DELETE but it is not possible with TRUNCATE.
850
+
* If table is referenced by any foreign key constraints then TRUNCATE cannot work.
851
+
* TRUNCATE is faster than DELETE, because when you use DELETE to delete the data, at that time it store the whole data in rollback space from where you can get the data back after deletion, whereas TRUNCATE will not store data in rollback space and will directly delete it. You can'\t get the deleted data back when you use TRUNCATE.
852
+
853
+
<div align="right">
854
+
<b><a href="#table-of-contents">↥ back to top</a></b>
855
+
</div>
856
+
857
+
## # 11. SQL Keys
858
+
859
+
<br/>
860
+
861
+
## Q. What is the difference between primary and foreign key?
862
+
863
+
*Primary key uniquely identify a relationship in a database, whereas foreign key is the key that is in other relation and it has been referenced from the primary keyfrom other table.
864
+
865
+
*Primary key remains one only for the table, whereas there can be more than one foreign key.
866
+
867
+
*Primary key is unique and won'\t be shared between many tables, but foreign key will be shared between more than one table and will be used to tell the relationship between them.
868
+
869
+
<div align="right">
870
+
<b><a href="#table-of-contents">↥ back to top</a></b>
871
+
</div>
872
+
873
+
#### Q. What is a unique key?
874
+
#### Q. What is a foreign key of a database?
875
+
#### Q. What is a constraint in SQL?
876
+
#### Q. How do I define constraints in SQL?
877
+
#### Q. What is a candidate key?
878
+
#### Q. What is the default index created on primary key in sql server?
879
+
880
+
*ToDo*
881
+
882
+
<div align="right">
883
+
<b><a href="#table-of-contents">↥ back to top</a></b>
884
+
</div>
885
+
886
+
## # 12. SQL Join
887
+
888
+
<br/>
889
+
890
+
## Q. Explain JOIN Query in mySQL?
891
+
892
+
A `JOIN` clause is used to combine rows from two or more tables, based on a related column between them.
893
+
Take `users` table and `orders` table for example.
894
+
895
+
**Users Table:**
896
+
897
+
|user_id|name|mobile|
898
+
|---|---|---|
899
+
|1|John|123|
900
+
|2|Joe|124|
901
+
902
+
**Orders Table:**
903
+
904
+
|order_id|user_id|total|created_at|
905
+
|---|---|---|---|
906
+
|1|1|500|2022-12-19 18:32:00|
907
+
|2|1|800|2021-12-03 08:32:00|
908
+
|3|2|50|2020-12-13 12:49:00|
909
+
|4|1|80|2021-12-15 21:19:00|
910
+
911
+
So to get the list of orders with names and mobile nos. for each order, we can join `orders` and `users` on the basis of `user_id`.
912
+
913
+
```sql
914
+
SELECT
915
+
o.*,
916
+
u.name,
917
+
u.mobile
918
+
FROM
919
+
ordes o
920
+
JOIN users u ON o.user_id = u.user_id;
921
+
```
922
+
923
+
<div align="right">
924
+
<b><a href="#table-of-contents">↥ back to top</a></b>
925
+
</div>
926
+
927
+
## Q. Explain the different types of joins?
928
+
929
+
Using Join in a query, we can retrieve referenced columns or rows from multiple tables.
930
+
931
+
Following are different types of Joins:
932
+
933
+
1. JOIN: Return details from tables if there is at least one matching row in both tables.
934
+
2. LEFT JOIN: It will return all rows from the left table, even if there are no matching row in the right table.
935
+
3. RIGHT JOIN: It will return all rows from the right table, even if there is no matching row in the left table.
936
+
4. FULL JOIN: It will return rows when there is a match in either of tables.
937
+
938
+
<div align="right">
939
+
<b><a href="#table-of-contents">↥ back to top</a></b>
940
+
</div>
941
+
942
+
## Q. What are Self Join and Cross Join?
943
+
944
+
* When we want to join a table to itself then SELF JOIN is used.
945
+
946
+
* We can give one or more aliases to eliminate the confusion.
947
+
948
+
* A self join can be used as any type, if both the tables are same.
949
+
950
+
* The simple example where we can use SELF JOIN is if in a company have a hierarchal reporting structure and an employee reports to another.
951
+
952
+
* A cross join give the number of rows in the first table multiplied by the number of rows in second table.
953
+
954
+
* The simple example where we can use CROSS JOIJ is if in an organization wants to combine every Employee with family table to see each Employee with each family member.
955
+
956
+
<div align="right">
957
+
<b><a href="#table-of-contents">↥ back to top</a></b>
| 01. |SELECT c1, c2 FROM t1 INNER JOIN t2 on condition|Select columns c1 and c2 from a table named t1 and perform an inner join between t1 and t2 |
965
+
| 02. |SELECT c1, c2 FROM t1 LEFT JOIN t2 on condition|Select columns c1 and c2 from a table named t1 and perform a left join between t1 and t2|
966
+
| 03. |SELECT c1, c2 FROM t1 RIGHT JOIN t2 on condition|Select columns c1 and c2 from a table named t1 and perform a right join between t1 and t2|
967
+
| 04. |SELECT c1, c2 FROM t1 FULL OUTER JOIN t2 on condition|Select columns c1 and c2 from a table named t1 and perform a full outer join between t1 and t2|
968
+
| 05. |SELECT c1, c2 FROM t1 CROSS JOIN t2 |Select columns c1 and c2 from a table named t1 and produce a Cartesian product of rows in tables|
969
+
| 06. |SELECT c1, c2 FROM t1, t2 |Select columns c1 and c2 from a table named t1 and produce a Cartesian product of rows in tables|
970
+
| 07. |SELECT c1, c2 FROM t1 A INNER JOIN t2 B on condition |Select columns c1 and c2 from a table named t1 and joint it to itself using an INNER JOIN clause|
971
+
972
+
## Q. What is full join in SQL?
973
+
974
+
A **FULL JOIN** (or **FULL OUTER JOIN**) in SQL returns all records when there is a match in either the left or right table. It combines the results of both LEFT JOIN and RIGHT JOIN. When there is no match, NULL values are returned for columns from the table that lacks a match.
975
+
976
+
Example:
977
+
```sql
978
+
SELECT *
979
+
FROM TableA
980
+
FULL JOIN TableB
981
+
ON TableA.id = TableB.id;
982
+
```
983
+
984
+
## Q. What is an outer join in SQL?
985
+
986
+
An **OUTER JOIN** in SQL refers to joins that return not only the rows with matching keys in both tables but also the rows with no corresponding key in one of the tables. There are three types of OUTER JOINS:
987
+
- **LEFT OUTER JOIN**: Returns all rows from the left table, and the matched rows from the right table. If no match is found, NULL values are returned for columns from the right table.
988
+
- **RIGHT OUTER JOIN**: Returns all rows from the right table, and the matched rows from the left table. If no match is found, NULL values are returned for columns from the left table.
989
+
- **FULL OUTER JOIN**: Returns all rows when there is a match in either table, filling in NULLs when there are no matches.
990
+
991
+
## Q. What is an inner join in SQL?
992
+
993
+
An **INNER JOIN** in SQL returns only the rows where there is a match in both tables. It excludes rows that do not have matching values in both tables.
994
+
995
+
Example:
996
+
```sql
997
+
SELECT *
998
+
FROM TableA
999
+
INNER JOIN TableB
1000
+
ON TableA.id = TableB.id;
1001
+
```
1002
+
1003
+
## Q. What is left join in SQL Server?
1004
+
1005
+
A **LEFT JOIN** (or **LEFT OUTER JOIN**) in SQL Server returns all records from the left table (TableA), and the matched records from the right table (TableB). If there is no match, the result is NULL on the side of the right table.
1006
+
1007
+
Example:
1008
+
```sql
1009
+
SELECT *
1010
+
FROM TableA
1011
+
LEFT JOIN TableB
1012
+
ON TableA.id = TableB.id;
1013
+
```
1014
+
1015
+
## Q. What is a right join in SQL Server?
1016
+
1017
+
A **RIGHT JOIN** (or **RIGHT OUTER JOIN**) in SQL Server returns all records from the right table (TableB), and the matched records from the left table (TableA). If there is no match, the result is NULL on the side of the left table.
1018
+
1019
+
Example:
1020
+
```sql
1021
+
SELECT *
1022
+
FROM TableA
1023
+
RIGHT JOIN TableB
1024
+
ON TableA.id = TableB.id;
1025
+
```
1026
+
1027
+
## Q. What is the default join in SQL?
1028
+
1029
+
The **default join** in SQL is the **INNER JOIN**. When you perform a JOIN without specifying the type, SQL assumes it to be an INNER JOIN, meaning it will only return rows where there is a match in both tables.
1030
+
1031
+
Example:
1032
+
```sql
1033
+
SELECT *
1034
+
FROM TableA
1035
+
JOIN TableB
1036
+
ON TableA.id = TableB.id;
1037
+
```
1038
+
821
1039
<div align="right">
822
1040
<b><a href="#table-of-contents">↥ back to top</a></b>
0 commit comments