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: Copy 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 … 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: Copy 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: Copy SELECT column_1, column_2,… FROM table_1 UNION ALL SELECT column_1, column_2,… FROM table_2 … Read more

Join Tables from Different Databases in SQL Server

Here is the general syntax that you may use to join tables from two different databases in SQL Server: Copy SELECT tb_1.*, tb_2.* FROM [database_1].[table_schema].[table_name_1] tb_1 JOIN [database_2].[table_schema].[table_name_2] tb_2 ON tb_1.id = tb_2.id In the next section, you’ll see how to join two tables from two different databases in SQL Server. The tables and databases … 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: Copy SELECT column_1, column_2, column_3,… INTO #name_of_temp_table FROM table_name WHERE condition (2) The CREATE TABLE approach: Copy CREATE TABLE #name_of_temp_table ( column_1 datatype, column_2 datatype, column_3 datatype, . . column_n datatype ) Note that once you created the … Read more

How to Get the Max Value using SQL

To find the max value using SQL: Copy SELECT MAX(field_name) AS new_field_name FROM table_name Next, you’ll see how to find the max value across the following 3 scenarios: Under a single field After joining tables After using group by (1) Find the Max Value Under a Single Field To start with a simple example, let’s … 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): Copy SELECT tb1.*, tb2.* FROM left_table_name tb1 LEFT JOIN right_table_name tb2 ON … Read more

Update Table Records using SQL

You can use the query below in order to update records using SQL: Copy 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 … 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: Copy DELETE FROM table_name WHERE condition (2) Delete all the records in a given table: Copy DELETE FROM table_name In the next section, you’ll see how to apply the above approaches using a practical example. The … Read more

Insert Records Into a Table using SQL

Here is the general syntax that you can use in order to insert records into a table using SQL: Copy 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 … 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: Copy 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 … Read more