Add a Column to an Existing Table using ALTER TABLE

Here is the syntax to add a column to an existing table using ALTER TABLE: ALTER TABLE table_name ADD new_column_name data_type Next, you’ll see how to apply the above syntax using a simple example. Add a Column to an Existing Table using ALTER TABLE Suppose that you have a table called products. The ‘products’ table currently … Read more

How to Apply UNION and UNION ALL using SQL

You can use the following syntax in order to apply UNION and UNION ALL using SQL: (1) Apply UNION while removing duplicates: SELECT column_1, column_2,… FROM table_1 UNION SELECT column_1, column_2,… FROM table_2 (2) Apply UNION ALL while keeping the duplicates: SELECT column_1, column_2,… FROM table_1 UNION ALL SELECT column_1, column_2,… FROM table_2 Next, you’ll … Read more

Join Tables from Different Databases in SQL Server

Here is a template that you can use to join tables from two different databases in SQL Server: SELECT table_1.*, table_2.* FROM [Database_1].[Table_Schema].[Table_Name_1] table_1 JOIN [Database_2].[Table_Schema].[Table_Name_2] table_2 ON table_1.id = table_2.id In the next section, you’ll see how to join two tables from two different database in SQL Server. The tables and databases will be … Read more

How to Create a Temporary Table in SQL Server

Here are two approaches to create a temporary table in SQL Server: (1) The SELECT INTO approach: SELECT Column1,Column2,Column3,… INTO #NameOfTempTable FROM [DatabaseName].[TableSchema].[TableName] WHERE condition (2) The CREATE TABLE approach: CREATE TABLE #NameOfTempTable ( Column1 datatype, Column2 datatype, Column3 datatype, . . ColumnN datatype ) Note that once you created the table under the second … Read more

How to Get the Max Value in SQL Server

You can find the max value in SQL Server using: SELECT max(field_name) as new_field_name FROM [database_name].[table_schema].[table_name] Next, you’ll see how to find the max value across the following 3 scenarios: Under a Single Field After Joining Tables Using Group By (1) Find the Max Value Under a Single Field in SQL Server To start with … Read more

LEFT JOIN using SQL (examples included)

You can use a LEFT JOIN to get all the records from the left table, and the records from the right table where there is a match (if there is no match, you’ll get NULL under the selected fields from the right table): SELECT tb1.*, tb2.* FROM left_table_name tb1 LEFT JOIN right_table_name tb2 ON tb1.id … Read more

Update Table Records using SQL

You can use the following query in order to update records using SQL: UPDATE table_name SET column_1 = value_1, column_2 = value_2, … WHERE condition Next, you’ll see how to apply the above query using a simple example. The Example Let’s suppose that you created a table, where: The table name is: product The table … Read more

Delete Records from a Table using SQL

Here are two approaches to delete records from a table using SQL: (1) Delete records based on specified conditions: DELETE FROM table_name WHERE condition (2) Delete all the records in a given table: DELETE FROM table_name In the next section, you’ll see how to apply the above approaches using a practical example. The Example: Suppose … Read more

Insert Records Into a Table using SQL

Here is the syntax that you can use in order to insert records into a table using SQL: INSERT INTO table_name (column_1, column_2, column_3,…) VALUES (‘value_1’, ‘value_2’, ‘value_3′,…) Let’s now review an example to see how to apply the above syntax in practice. The Example Let’s suppose that you created an empty table called the … Read more

How to Get the Data Type of Columns in SQL Server

You can use the following query to get the data type of your columns in SQL Server: SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS Next, you’ll see 3 scenarios to get the data type: of all columns in a particular database of all columns in a particular table for a specific column 3 Scenarios … Read more