DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Why SQL Isn’t the Right Fit for Graph Databases
  • SQL Commands: A Brief Guide
  • MongoDB to Couchbase for Developers, Part 1: Architecture
  • FHIR Data Model With Couchbase N1QL

Trending

  • Advancing Robot Vision and Control
  • The Perfection Trap: Rethinking Parkinson's Law for Modern Engineering Teams
  • Understanding the Shift: Why Companies Are Migrating From MongoDB to Aerospike Database?
  • Supervised Fine-Tuning (SFT) on VLMs: From Pre-trained Checkpoints To Tuned Models
  1. DZone
  2. Data Engineering
  3. Databases
  4. SQL Data Manipulation Language (DML) Operations: Insert, Update, Delete

SQL Data Manipulation Language (DML) Operations: Insert, Update, Delete

This article covers SQL DML operations like insert, update, delete, and more, including drop, distinct, and group by.

By 
Madhuri Hammad user avatar
Madhuri Hammad
·
Oct. 17, 23 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
14.1K Views

Join the DZone community and get the full member experience.

Join For Free

Imagine yourself as the master conductor of a symphony orchestra, and the database is your musical score. With SQL's DML operations, you hold the baton that allows you to add, modify, and remove the harmonious notes that compose your database. Yes, you read that right! This may sound like a dream, but SQL (Structured Query Language) turns this dream into reality.

SQL, as we all know, is not just any ordinary tool; it’s a game-changing tool that has the ability to transform how we manage and manipulate relational databases. With the power of SQL, you open up a world of endless possibilities for handling and controlling your valuable data. SQL includes Data Manipulation Language (DML), a powerful set of operations like Insert, Update, and Delete,  that controls access to data and to the database.  It allows you to mold and shape your data in various ways. With just a few lines of code, you can easily and quickly add new data, modify or update existing data, and remove unnecessary data from database tables. Whether you're an experienced professional or a novice, understanding these SQL operations will give you the ability to turn your data to meet your specific requirements. 

In this article, we will explore various DML operations and understand how they help achieve effective data management. If you looking for more query questions in SQL, check out SQL Query Interview Questions and Answers that help you prepare for Interviews. By the end, you'll have a better understanding of how these SQL operations effectively covert or alter your data in a meaningful way. So, let's dive in together and discover what data manipulation language holds in SQL!

SQL Data Manipulation Language (DML) Operations

Let’s embark on an adventure through the captivating landscape of SQL Data Manipulation Language (DML) operations: Insert, Update, and Delete.

Command
Description
INSERT Adding new records to a table
UPDATE Modifying existing data
DELETE Removing records


NOTE: DML commands, like DELETE, are not automatically saved in the database, which means changes can be rolled back. To prevent loss of information, we use the COMMIT command to make the changes permanent in the database tables. To make all changes permanent, you can use the COMMIT statement at the end of your DML command.

Now, let’s try to understand each of the DML commands stated above in more detail:

1. Insert Operation

It allows users to add new data into rows of a database table. Insert operation is important as it allows us to fill tables with essential data. 

Syntax:

SQL
 
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);


We can also specify the value of data to be inserted without including the column names.

SQL
 
INSERT INTO table_name    
VALUES (value1, value2, .... valuen);  


Here:

  • Table_name represents the name of the table into which data will be inserted. 
  • "column1, column2, column3, …” represent the specific columns to which values will be inserted. 
  • "value1, value2, value3, ..." represent actual values that will be inserted into the specified columns.

SQL Query Example:

Let’s consider a table called "Employees" with columns "EmployeeID," "FirstName," "LastName", “Email”, “PhoneNo”, and “Salary”. To insert a new employee record, we can use the following SQL statement:

SQL
 
INSERT INTO Employees (EmployeeID, FirstName, LastName, Email, Country, Salary)
VALUES (1, 'Navya', ‘John', 'navyajohn@email.com', 'USA', 50000.00),
       (2, 'Kunal', 'Swami', 'kunalswami@email.com', 'UK', 44000.00),
       (3, 'Jai', 'Doneriya', 'jaidoneriya@email.com', 'India', 35000.00);


Output:

2. Update Operation

It allows users to modify or update existing data within a table. It generally offers the flexibility to update or modify the values of one or more records in the table. 

Syntax:

SQL
 
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;


Here:

  • "table_name" represents the name of the table to be updated. 
  • "column1 = value1, column2 = value2, ..." specifies the columns to be updated along with their new values. 
  • "WHERE" clause is optional. The update will be applied to all records in the table unless the user specifies a "WHERE" clause.

SQL Query Example:

Suppose want to update the FirstName of the employee with EmployeeID 1 to 'Smith' and Email with ‘smithjohn@gmail.com’. The UPDATE statement would look like this:

SQL
 
UPDATE Employees
SET LastName = 'Smith', Email= ‘smithjohn@gmail.com’
WHERE EmployeeID = 1;


Output:


3. Delete Operation

It allows users to remove/delete records or sets of records from the database tables depending upon the condition specified in the WHERE clause.

Syntax:

SQL
 
DELETE FROM table_name
WHERE condition;


Here:

  • "table_name" represents the name of the table from which records should be deleted. 
  • "WHERE" clause is used to determine which records should be deleted. Without a "WHERE" clause, all records from the specified table will be removed. 

SQL Query Example:

Suppose we want to delete all employees whose EmployeeID is greater than and equal to 2. The DELETE Command would look like:

SQL
 
DELETE FROM Employees
WHERE EmployeeID >= 2;


Output:


Other Operations

Discover powerful commands and techniques that will enhance your SQL skills. Let's dive in!

1. Select Statement 

The Select statement is the most widely used DML command to retrieve data from a table. It allows you to select specific columns you want to retrieve, apply filters, and even sort the results in the desired way. The data returned is stored in a new table called the result set.

Syntax:

SQL
 
SELECT column1, column2 FROM table_name WHERE condition ORDER BY column ASC/DESC;


2. Join Operation

Join operations allow you to combine data from multiple tables based on a common column. It enables you to fetch related data and create meaningful connections between tables.

Syntax:

SQL
 
SELECT column1, column2 FROM table1 JOIN table2 ON table1.column = table2.column;


3. Aggregate Functions

Aggregate functions perform calculations on sets of values and return a single value. They are useful for obtaining summarized information from a table, such as finding the sum, average, maximum, or minimum value.

Syntax:

SQL
 
SELECT aggregate_function(column) FROM table_name;


4. Subqueries

Subqueries are queries embedded within another query. They allow you to use the result of one query as a condition in another query, providing advanced filtering and data manipulation capabilities.

Syntax:

SQL
 
SELECT column1 FROM table_name WHERE column2 IN (SELECT column3 FROM table2 WHERE condition);


5. Group By 

The Group By clause is used to group rows based on one or more columns. It is often combined with aggregate functions to perform calculations on each group separately.

Syntax:

SQL
 
SELECT column1, aggregate_function(column2) FROM table_name GROUP BY column1;


6. Having Clause

The Having clause works in conjunction with the Group By clause. It allows you to apply filters to the grouped data based on specific conditions.

Syntax:

SQL
 
SELECT column1, aggregate_function(column2) FROM table_name GROUP BY column1 HAVING condition;


7. Distinct

The Distinct keyword retrieves only distinct (unique) values from a column, as it effortlessly eliminates duplicates from the result set returned by the SQL Query. It ensures that the result set contains only one instance of each value.

Syntax:

SQL
 
SELECT DISTINCT column FROM table_name;


8. Drop-Table

The Drop Table statement is used to delete an entire table from a database. This operation permanently removes the table and all its associated data.

Syntax:

SQL
 
DROP TABLE table_name;


Conclusion

All DML operations mentioned above, like Insert, Update, Delete, and more, are essential for managing and manipulating data in SQL databases. Users can add new data, update/modify existing data, and remove unnecessary data from the tables using DML Operations. Therefore, having a deep understanding of the syntax and functionality of DML operations is essential for users to effectively control and maintain data integrity. Remember, becoming proficient in DML operations offers you powerful capabilities for data management, simplifying the handling of data-driven applications and ensuring the reliability of information in relational databases.

Data manipulation language Database Query language Relational database Data (computing) sql

Opinions expressed by DZone contributors are their own.

Related

  • Why SQL Isn’t the Right Fit for Graph Databases
  • SQL Commands: A Brief Guide
  • MongoDB to Couchbase for Developers, Part 1: Architecture
  • FHIR Data Model With Couchbase N1QL

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!