How do I UPDATE from a SELECT in SQL Server?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for How do I UPDATE from a SELECT in SQL Server?

šŸ“ Tech Blog: Updating a Table with SELECT in SQL Server

šŸ‘‹ Hey there fellow tech enthusiasts! Today, we're diving into a commonly asked question in the world of SQL Server: "How do I update a table using SELECT?" šŸ¤”

When it comes to SQL Server, we all know it's possible to insert rows into a table using an INSERT.. SELECT statement. But can we use a SELECT statement to update a table? Let's find out! šŸ’”

šŸ” The Scenario: Imagine you have a temporary table with some values, and you want to update another table using those values. Let's say we have a table called Table, and we want to update the col1 and col2 columns based on the values in the other_table table. Our initial attempt might look something like this:

UPDATE Table SET col1, col2
SELECT col1, col2 
FROM other_table 
WHERE sql = 'cool'
WHERE Table.id = other_table.id

šŸ›‘ The Problem: But hold your horses! šŸŽ Unfortunately, this syntax won't work and will throw a syntax error. Updating a table with values obtained from a SELECT statement requires a slightly different approach.

šŸ’” The Solution: Fortunately, SQL Server provides a handy alternative using the FROM clause in combination with the UPDATE statement.

Here's the correct syntax to update Table using the values from other_table:

UPDATE Table
SET col1 = other_table.col1,
    col2 = other_table.col2
FROM other_table
WHERE Table.id = other_table.id
  AND other_table.sql = 'cool'

✨ Explanation: Let's break down the updated syntax:

  1. We use the UPDATE statement to specify the table we want to update, which is Table in our case.

  2. Next, we use the SET clause to assign values to the columns we want to update. We assign the values from other_table.col1 to Table.col1, and similarly for col2.

  3. The FROM clause comes into play, allowing us to join other_table with Table based on the common id column.

  4. Finally, we add a WHERE clause to filter the rows we want to update. In this example, we're updating rows where other_table.sql is equal to 'cool'.

šŸš€ Time to Shine! With this updated syntax, you can now confidently update a table using values from a SELECT statement in SQL Server. šŸŽ‰

So go ahead, test out the code, and start updating your tables like a pro! šŸ’Ŗ And don't hesitate to share your experiences or any questions you may have in the comments below. Let's level up our SQL game together! šŸ˜„

šŸ™Œ Call-to-Action: If you found this blog post helpful, give it a thumbs up šŸ‘ and share it with your fellow SQL Server enthusiasts. Let's spread the knowledge! šŸ’” And don't forget to subscribe to our newsletter to stay updated with the latest tech tips and tricks.

Thanks for reading, and happy SQL updating! šŸŽŠāœØ

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

šŸ”„ šŸ’» šŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! šŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings šŸ’„āœ‚ļø Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide šŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? šŸ¤” Well, my