For database management, you've gotta understand the fundamental operations in SQL that allow you to create new tables, insert or update new records, tweak information in columns, and drop tables where necessary.
These types of actions are known as CRUD operations and use the Data Manipulation Language (DML) commands , , , and :
To understand each CRUD command, we'll use the table from the Amazon database in this Amazon SQL Interview Question to test out the queries. While we'll provide sample queries and expected results, you won't be able to run these queries as it's not a live database. Our SQL interview platform can only run queries!
table which stores Amazon product reviews:
review_id | user_id | submit_date | product_id | stars |
---|---|---|---|---|
1200 | 100 | 05/17/2022 00:00:00 | 25255 | 4 |
1301 | 120 | 05/18/2022 00:00:00 | 25600 | 4 |
2501 | 142 | 06/21/2022 00:00:00 | 12580 | 5 |
2536 | 136 | 07/04/2022 00:00:00 | 11223 | 5 |
2555 | 232 | 05/31/2022 00:00:00 | 25600 | 4 |
2556 | 167 | 05/31/2022 00:00:00 | 25600 | 5 |
4517 | 981 | 07/05/2022 00:00:00 | 69852 | 2 |
4582 | 562 | 06/15/2022 00:00:00 | 12580 | 4 |
5293 | 362 | 06/18/2022 00:00:00 | 50001 | 3 |
6171 | 123 | 06/08/2022 00:00:00 | 50001 | 4 |
6352 | 192 | 07/26/2022 00:00:00 | 69852 | 3 |
7802 | 265 | 06/10/2022 00:00:00 | 69852 | 4 |
Let's say, Amazon wants to maintain information of their customers. This allows the store to track customer interactions, personalize marketing efforts, and provide better customer service.
The following query creates a new table named with customer information such as customer ID, first name, last name, email, and phone number.
Query Example:
Expected Result:
A new table named is created with the column serving as the primary key for uniquely identifying each customer.
customer_id | first_name | last_name | phone_number | |
---|---|---|---|---|
... | ... | ... | ... | ... |
After creating the table, we insert three new records of customers.
Query Example:
Expected Results:
These three customers' records are inserted into the table, each with a unique customer ID and corresponding first name, last name, email and phone number values.
customer_id | first_name | last_name | phone_number | |
---|---|---|---|---|
1 | Nick | Singh | nick.singh@datalemur.com | 123-456-7890 |
2 | Kevin | Huo | kevin.huo@datalemur.com | 987-654-3210 |
3 | Ken | Jee | ken.jee@datalemur.com | 357-654-5467 |
CREATE Example: Inserting a New Review
A customer has left a new product review on the Amazon website. Let's add it to the table.
Query Example:
Expected Result:
A new review is inserted into the table:
review_id | user_id | submit_date | product_id | stars |
---|---|---|---|---|
... | ... | ... | ... | ... |
6171 | 123 | 06/08/2022 00:00:00 | 50001 | 4 |
6352 | 192 | 07/26/2022 00:00:00 | 69852 | 3 |
7802 | 265 | 06/10/2022 00:00:00 | 69852 | 4 |
8125 | 777 | 01/08/2022 00:00:00 | 12580 | 5 |
Now, let's say you want to retrieve all the reviews for a particular product item with the ID 12580.
Query Example:
Expected Result:
All reviews for the product with ID 12580 is retrieved from the table:
review_id | user_id | submit_date | product_id | stars |
---|---|---|---|---|
2501 | 142 | 06/21/2022 00:00:00 | 12580 | 5 |
4582 | 562 | 06/15/2022 00:00:00 | 12580 | 4 |
8125 | 777 | 2022-08-01 00:00:00 | 12580 | 5 |
Imagine you need to adjust the star rating of a specific review (identified by review ID 1301) from 4 stars to 3 stars.
Query Example:
Expected Result:
Before the operation:
review_id | user_id | submit_date | product_id | stars |
---|---|---|---|---|
1301 | 120 | 05/18/2022 00:00:00 | 25600 | 4 |
After the operation, the rating of the review ID 1301 is updated to 3 stars:
review_id | user_id | submit_date | product_id | stars |
---|---|---|---|---|
1301 | 120 | 05/18/2022 00:00:00 | 25600 | 3 |
DELETE Example: Removing an Existing Review
Suppose you need to delete a particular review identified by review ID 4517 from the table.
Query Example:
Expected Result:
Before the operation:
review_id | user_id | submit_date | product_id | stars |
---|---|---|---|---|
... | ... | ... | ... | ... |
2556 | 167 | 05/31/2022 00:00:00 | 25600 | 5 |
4517 | 981 | 07/05/2022 00:00:00 | 69852 | 2 |
4582 | 562 | 06/15/2022 00:00:00 | 12580 | 4 |
After the operation, review ID 4517 is removed from the table:
review_id | user_id | submit_date | product_id | stars |
---|---|---|---|---|
... | ... | ... | ... | ... |
2556 | 167 | 05/31/2022 00:00:00 | 25600 | 5 |
[Deleted review ID 4517] | ... | ... | ... | ... |
4582 | 562 | 06/15/2022 00:00:00 | 12580 | 4 |
The operation is used to remove database objects such as tables, indexes, views, or even entire databases. It is a Data Definition Language (DDL) command.
Using the DROP operation requires caution, as it ‼️ permanently deletes database objects and their associated data ‼️.
DROP Example: Removing a Table
Consider a scenario where you must permanently remove the table along with all its contents from the database.
Query Example:
Expected Result:
The table and all its contents is now permanently deleted from the database. Since the table is dropped, there won't be any data to display.
The operation is used to modify the structure of existing database objects. It allows adding, modifying, or dropping columns, constraints, or indexes from a table. It is also a Data Definition Language (DDL) command.
ALTER Example: Adding a New Column for Product Reviews
Imagine you want to enhance the structure of the table by introducing a new column called which will store product reviews.
Query Example:
Expected Result:
The table's structure is modified to include a new column named of type . However, since the column is just added and not populated, there won't be any data to display initially.
review_id | user_id | submit_date | product_id | stars | review_text |
---|---|---|---|---|---|
1200 | 100 | 05/17/2022 00:00:00 | 25255 | 4 | |
1301 | 120 | 05/18/2022 00:00:00 | 25600 | 4 | |
2501 | 142 | 06/21/2022 00:00:00 | 12580 | 5 | |
2536 | 136 | 07/04/2022 00:00:00 | 11223 | 5 | |
2555 | 232 | 05/31/2022 00:00:00 | 25600 | 4 | |
... | ... | ... | ... | ... |
Suppose you want to update the data type of the column in the table. Currently, it stores integer values like 3, 4, 5, but you need it to store floating-point numbers like 3.5, 4.4, 5.0.
Query Example:
Expected Result:
The data type of the column in the table is changed from integer to float.
Imagine you need to rename the column in the table. You might want to change it to for better clarity and consistency.
Query Example:
Expected Result:
The column in the table is renamed to .
review_id | user_id | submission_date | product_id | stars | review_text |
---|---|---|---|---|---|
... | ... | ... | ... | ... |
Let's say, you need to increase the maximum length of the column in the table. Currently, it can only store limited text. You want to expand it to accommodate longer reviews, allowing up to 1000 characters.
Query Example:
Expected Result:
The maximum length of the column in the table is increased to 1000 characters.
DML is concerned with modifying or manipulating data in the database, while DQL (Data Query Language) is focused on retrieving or querying data from the database. While DML was covered briefly in this article, we created a whole free SQL tutorial to practice your query skills: