Skip to content

Commit 04c7743

Browse files
committed
added new answers
1 parent 4b9c23f commit 04c7743

1 file changed

Lines changed: 221 additions & 1 deletion

File tree

README.md

Lines changed: 221 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,4 +465,224 @@ Note that depending on your SQL environment, you may need to have appropriate pe
465465

466466
## # 4. SQL Table
467467

468-
<br/>
468+
<br/>
469+
470+
#### Q. How to create a table in SQL?
471+
472+
To create a table in SQL, you can use the `CREATE TABLE` statement. The basic syntax for creating a table is as follows:
473+
474+
```sql
475+
CREATE TABLE table_name (
476+
column1 datatype constraint,
477+
column2 datatype constraint,
478+
column3 datatype constraint,
479+
...
480+
columnN datatype constraint
481+
);
482+
```
483+
Here's a breakdown of the syntax:
484+
485+
. `CREATE TABLE` is the SQL keyword that specifies you want to create a table.
486+
. `table_name` is the name you want to give to your table.
487+
`(column1, column2, ..., columnN)` is a comma-separated list of column names you want to create in your table.
488+
.`datatype` is the data type you want to assign to each column.
489+
.`constraint` is an optional condition or rule that you want to set on a column.
490+
491+
For example, if you want to create a simple table with two columns called `users` and `email`, where `users` is of type `VARCHAR(50)` and `email` is of type `VARCHAR(100)`, you can use the following SQL statement:
492+
493+
```sql
494+
CREATE TABLE user_info (
495+
users VARCHAR(50),
496+
email VARCHAR(100)
497+
);
498+
```
499+
Note that different SQL database management systems may have slightly different syntax or rules for creating tables, so it's always a good idea to consult the documentation for the specific database you're using.
500+
501+
#### Q. What are tables and Fields?
502+
503+
In a relational database management system (RDBMS), a table is a collection of data organized in rows and columns. Tables are the primary means to store data in a relational database. Each table represents an entity, such as a customer, product, or transaction, and each row in the table represents a single instance or record of that entity. For example, a table named "Customers" might contain fields such as "CustomerID", "FirstName", "LastName", "Address", and "Phone".
504+
505+
Fields, also known as columns or attributes, are the individual elements of a table that hold specific pieces of data for each row. Each field represents a specific characteristic or property of the entity the table represents. For example, in a table of customers, fields might include "CustomerID", "FirstName", "LastName", "Address", and "Phone". Each row in the table represents a single customer, and each field in that row contains a specific piece of information about that customer, such as their name or address.
506+
507+
#### Q. How to delete a table in SQL Server?
508+
509+
To delete a table in SQL Server, you can use the `DROP TABLE` statement. The syntax is as follows:
510+
511+
```sql
512+
DROP TABLE table_name;
513+
```
514+
Here, `table_name` is the name of the table you want to delete.
515+
516+
For example, to delete a table called "customers", you would use the following SQL statement:
517+
518+
```sql
519+
DROP TABLE customers;
520+
```
521+
Keep in mind that when you delete a table, all the data stored in that table is also deleted, and it cannot be recovered. Therefore, make sure that you have a backup of the table and its data before deleting it.
522+
523+
#### Q. What is the difference between DELETE TABLE and TRUNCATE TABLE commands?
524+
525+
DELETE TABLE and TRUNCATE TABLE are both SQL commands used to remove data from a table, but there are differences between them:
526+
527+
.DELETE TABLE is a DML (Data Manipulation Language) command that removes all rows from a table based on a condition, or removes all rows if no condition is specified. DELETE TABLE is slower than TRUNCATE TABLE because it maintains a transaction log, which can be rolled back in case of errors. DELETE TABLE is useful when you want to remove specific rows from a table.
528+
529+
.TRUNCATE TABLE is a DDL (Data Definition Language) command that removes all rows from a table without logging the individual row deletions. TRUNCATE TABLE is faster than DELETE TABLE because it does not log every row deletion. TRUNCATE TABLE is useful when you want to remove all rows from a table, but preserve the table structure.
530+
531+
In summary, DELETE TABLE is used to remove specific rows from a table and maintains a transaction log, while TRUNCATE TABLE is used to remove all rows from a table and does not maintain a transaction log.
532+
533+
#### Q. What is the difference between TRUNCATE and DROP statements?
534+
535+
TRUNCATE and DROP are both SQL statements used to remove data from a table, but they have different functionalities.
536+
537+
TRUNCATE TABLE removes all data from the table, but leaves the table structure intact. It is a fast operation because it only deallocates the data pages used by the table, making it more efficient than using DELETE for large tables. However, it cannot be rolled back and it also resets the identity seed value of the table.
538+
539+
DROP TABLE, on the other hand, removes the entire table along with its structure and data, so it is a more drastic operation than TRUNCATE. It is useful when you want to completely remove a table and all its dependencies from the database. Like TRUNCATE, it cannot be rolled back.
540+
541+
In summary, TRUNCATE is a faster way to delete data from a table without destroying the table structure, while DROP TABLE is a more drastic way to remove a table and its dependencies from the database.
542+
543+
544+
#### Q. How to alter a table schema in SQL Server?
545+
546+
To alter a table schema in SQL Server, you can use the `ALTER TABLE` statement along with various sub-commands to modify the table structure. Here is the basic syntax:
547+
548+
```sql
549+
ALTER TABLE table_name
550+
[ADD | DROP] column_name datatype [NULL | NOT NULL] [DEFAULT default_value]
551+
[ADD | DROP] CONSTRAINT constraint_name constraint_definition
552+
```
553+
554+
The `ALTER TABLE `statement allows you to add or remove columns from an existing table, modify the data type or size of an existing column, and add or remove constraints. Here are some examples:
555+
556+
1. Add a new column to a table:
557+
558+
```sql
559+
ALTER TABLE employees
560+
ADD email VARCHAR(255) NOT NULL;
561+
```
562+
563+
2. Modify the data type of an existing column:
564+
565+
```sql
566+
ALTER TABLE employees
567+
ALTER COLUMN birthdate DATE;
568+
```
569+
570+
3. Drop a column from a table:
571+
572+
```sql
573+
ALTER TABLE employees
574+
DROP COLUMN email;
575+
```
576+
577+
4. Add a constraint to a table:
578+
579+
```sql
580+
ALTER TABLE employees
581+
ADD CONSTRAINT pk_employee PRIMARY KEY (employee_id);
582+
```
583+
584+
5. Drop a constraint from a table:
585+
586+
```sql
587+
ALTER TABLE employees
588+
DROP CONSTRAINT pk_employee;
589+
```
590+
Note that the specific syntax and options available for the `ALTER TABLE` statement may vary depending on the database management system you are using.
591+
592+
#### Q. What are Heap tables in SQL?
593+
594+
In SQL Server, a heap table is a table without a clustered index. In other words, it is a table that stores data without any particular order or structure. Instead of using a clustered index, a heap table uses a heap structure to store data.
595+
596+
When data is inserted into a heap table, it is added to the end of the table, without any specific ordering. When data is queried from a heap table, the SQL Server Database Engine must scan the entire table to find the requested data. This can be slow and inefficient, especially as the table grows larger.
597+
598+
Heap tables are useful for storing temporary or transient data that does not need to be stored in a specific order or accessed frequently. However, for large, frequently accessed tables, it is generally recommended to use a clustered index to improve performance.
599+
600+
601+
<div align="right">
602+
<b><a href="#table-of-contents">↥ back to top</a></b>
603+
</div>
604+
605+
## # 5. SQL Select
606+
607+
<br/>
608+
609+
#### Q. What are query types in a database?
610+
611+
In a database, there are typically three main types of queries:
612+
613+
1. Select Query: This type of query is used to retrieve data from a table or a set of tables. The SELECT statement is used to specify the columns to be retrieved and the table(s) from which to retrieve the data. This type of query is used most frequently in database applications.
614+
615+
2. Insert Query: This type of query is used to insert new data into a table. The INSERT statement is used to specify the values to be inserted into each column of the table.
616+
617+
3. Update Query: This type of query is used to modify existing data in a table. The UPDATE statement is used to specify which columns to modify and the new values for those columns. The WHERE clause is used to specify which rows to update.
618+
619+
There are also other types of queries, such as DELETE queries to delete data from a table, and JOIN queries to combine data from multiple tables, but these three are the most fundamental types of queries in a database.
620+
621+
#### Q. What is the difference between UNION and UNION ALL?
622+
623+
In SQL, `UNION` and `UNION ALL` are used to combine the results of two or more `SELECT` statements. The main difference between `UNION` and `UNION ALL` is that `UNION` removes any duplicate rows from the final result set, while `UNION` ALL retains all the rows, including duplicates.
624+
625+
Here's an example to illustrate the difference:
626+
627+
Suppose you have two tables, Table1 and Table2, with the following data:
628+
629+
Table1:
630+
631+
| Column1 | Column2 |
632+
|---------|---------|
633+
| A | 1 |
634+
| B | 2 |
635+
| C | 3 |
636+
637+
Table2:
638+
639+
| Column1 | Column2 |
640+
|---------|---------|
641+
| D | 4 |
642+
| B | 2 |
643+
| E | 5 |
644+
645+
If you run the following `UNION` query:
646+
647+
```sql
648+
SELECT * FROM Table1
649+
UNION
650+
SELECT * FROM Table2
651+
```
652+
The result will be:
653+
654+
| Column1 | Column2 |
655+
|---------|---------|
656+
| A | 1 |
657+
| B | 2 |
658+
| C | 3 |
659+
| D | 4 |
660+
| E | 5 |
661+
662+
663+
Notice that the duplicate row with Column1 = B has been removed from the final result set.
664+
665+
On the other hand, if you run the following `UNION ALL` query:
666+
667+
```sql
668+
SELECT * FROM Table1
669+
UNION ALL
670+
SELECT * FROM Table2
671+
```
672+
The result will be:
673+
674+
| Column1 | Column2 |
675+
|---------|---------|
676+
| A | 1 |
677+
| B | 2 |
678+
| C | 3 |
679+
| D | 4 |
680+
| B | 2 |
681+
| E | 5 |
682+
683+
In this case, the duplicate row with Column1 = B has been retained in the final result set.
684+
685+
686+
<div align="right">
687+
<b><a href="#table-of-contents">↥ back to top</a></b>
688+
</div>

0 commit comments

Comments
 (0)