w3resource

How to Insert a Single Record into a SQL Table?


Insert a Single Record

Write a SQL query to insert a single record into a table.

Solution:

-- Insert a new employee record into the "Employees" table.
INSERT INTO Employees (EmployeeID, Name, Age, Salary) -- Specify the columns to insert data into.
VALUES (1, 'Ziynet Safiyyah', 30, 50000); -- Provide values for the specified columns.

Explanation:

    1. Purpose of the Query :

    1. The goal is to insert a new record into the Employees table with specific values for the EmployeeID, Name, Age, and Salary columns.
    2. This demonstrates how to use the INSERT INTO statement to add a single row of data to a table.

    2. Key Components :

    1. INSERT INTO Employees : Specifies the table where the new record will be added.
    2. (EmployeeID, Name, Age, Salary) : Lists the columns in which data will be inserted.
    3. VALUES (1, 'Ziynet Safiyyah', 30, 50000) : Provides the corresponding values for the specified columns.

    3. Why use INSERT INTO? :

    1. The INSERT INTO statement is used to add new rows of data to a table, making it essential for populating tables with information.
    2. For example, when a new employee joins the company, their details can be added to the Employees table using this statement.

    4. Real-World Application :

    1. For example, in a company database, you might use this query to add a new employee named "Ziynet Safiyyah" with an age of 30 and a salary of 50,000.

Additional Notes:

  • Inserting records is a fundamental operation for managing data in relational databases.
  • Scenarios where inserting a single record is appropriate, such as:
    • Adding a new user, product, or transaction to the database.
    • Populating tables during initial setup or testing.
  • Important Considerations :
    • Ensure that the values provided match the data types and constraints of the specified columns.
    • If the table has an auto-incrementing primary key, you may omit that column from the INSERT INTO statement.

For more Practice: Solve these Related Problems:

  • Write a SQL query to insert a single record into the 'employees' table where one column's value is determined by a subquery that selects the highest salary from the 'salaries' table.
  • Write a SQL query to insert a single record into the 'products' table with one column computed by an arithmetic expression based on another column’s value.
  • Write a SQL query to insert a single record into a table with default constraints, explicitly providing values only for non-default columns.
  • Write a SQL query to insert a single record into the 'orders' table using a CASE expression to conditionally determine one column's value.
  • Write a SQL query to insert a single record into a partitioned table ensuring the partitioning key is correctly derived from given data.

Go to:


PREV : SQL DML Exercises Home
NEXT : Insert Multiple Records.

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.