logologo
  • AI Tools

    DB Query GeneratorMock InterviewResume BuilderLearning Path GeneratorCheatsheet GeneratorAgentic Prompt GeneratorCompany ResearchCover Letter Generator
  • XpertoAI
  • MVP Ready
  • Resources

    CertificationsTopicsExpertsCollectionsArticlesQuestionsVideosJobs
logologo

Elevate Your Coding with our comprehensive articles and niche collections.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Xperto-AI
  • Certifications
  • Python
  • GenAI
  • Machine Learning

Interviews

  • DSA
  • System Design
  • Design Patterns
  • Frontend System Design
  • ReactJS

Procodebase © 2024. All rights reserved.

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Understanding SQL Commands: SELECT, INSERT, UPDATE, DELETE

author
Generated by
Namit Sharma

02/08/2024

SQL

Sign in to read full article

SQL, or Structured Query Language, is the standard language for interacting with relational databases. It provides a way to communicate with the database and perform various operations. In this article, we'll delve into four essential SQL commands: SELECT, INSERT, UPDATE, and DELETE. Each command will be explained with practical examples to give you a solid understanding of their functionality.

1. SELECT Command

The SELECT command is used to retrieve data from a database. You can specify which columns of data you would like to see, as well as various filtering options to refine your search results.

Syntax:

SELECT column1, column2, ... FROM table_name WHERE condition;

Example:

Imagine we have a table named employees with the following structure:

idnamedepartmentsalary
1AliceHR70000
2BobEngineering80000
3CharlieMarketing65000
4DaveHR72000

To retrieve the names and salaries of employees from the HR department, you can execute:

SELECT name, salary FROM employees WHERE department = 'HR';

This will return:

namesalary
Alice70000
Dave72000

2. INSERT Command

The INSERT command is used to add new rows of data into a table. This command is essential for populating your database with initial data or any new records as they are created.

Syntax:

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

Example:

Continuing with the employees table, to add a new employee named "Eve" in the Engineering department with a salary of 85000:

INSERT INTO employees (name, department, salary) VALUES ('Eve', 'Engineering', 85000);

After executing the above command, the employees table will now have an additional row:

idnamedepartmentsalary
5EveEngineering85000

3. UPDATE Command

The UPDATE command is used to modify existing data within a table. It allows you to change one or multiple columns for rows that meet certain criteria.

Syntax:

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

Example:

Let’s say we want to give the employees in the Marketing department a raise of 5000. We can execute the following command:

UPDATE employees SET salary = salary + 5000 WHERE department = 'Marketing';

After this command runs, Charlie's original salary of 65000 will be updated to 70000:

idnamedepartmentsalary
3CharlieMarketing70000

4. DELETE Command

The DELETE command removes records from a table. It can delete specific rows that meet specified conditions, or, if no conditions are provided, it can remove all rows from the table.

Syntax:

DELETE FROM table_name WHERE condition;

Example:

Suppose an employee, Bob, has left the company, and we need to remove his record from the employees table. We can do that as follows:

DELETE FROM employees WHERE name = 'Bob';

After executing this, the employees table will no longer contain Bob’s record, and it will look like this:

idnamedepartmentsalary
1AliceHR70000
3CharlieMarketing70000
4DaveHR72000
5EveEngineering85000

Each of these commands plays a crucial role in managing a database effectively. Understanding how to properly compose and execute these SQL commands will enable you to interact with your data in a powerful and meaningful way.

Popular Tags

SQLDatabaseProgramming

Share now!

Like & Bookmark!

Related Collections

  • Mastering SQL: From Basics to Advanced

    19/09/2024 | SQL

Related Articles

  • Introduction to SQL Window Functions

    03/09/2024 | SQL

  • Understanding Data Retrieval with SELECT Statements in SQL

    19/09/2024 | SQL

  • Understanding Aggregate Functions in SQL

    19/09/2024 | SQL

  • Understanding SQL JOINs

    19/09/2024 | SQL

  • Managing and Securing SQL Databases

    03/09/2024 | SQL

  • Unlocking the Power of Advanced SQL Joins

    03/09/2024 | SQL

  • Deleting Data with the DELETE Statement in SQL

    19/09/2024 | SQL

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design