Retrieve results from a table based on conditions

I have a query with SQL as it needs an expertise. I have two tables Table A and B. Now I need to retrieve results from table 1 based on some conditions and i also need to retrieve results from table 1 based on results from table 2.

I have a query with SQL as it needs an expertise. I have two tables Table A and B. Now I need to retrieve results from table 1 based on some conditions and i also need to retrieve results from table 1 based on results from table 2.

I want to achieve

Select * from table1 where author ="xyz") + select * from table1 where id=""

--->id = select post_ID from table2 where author = "abc"

So the ID values of table1 matches the post_ID values of table 2

What are the differences between Standard SQL and Transact-SQL?

What are the differences between Standard SQL and Transact-SQL?

In this article, we'll explain syntax differences between standard SQL and the Transact-SQL language dedicated to interacting with the SQL

#1 Names of Database Objects

In relational database systems, we name tables, views, and columns, but sometimes we need to use the same name as a keyword or use special characters. In standard SQL, you can place this kind of name in quotation marks (""), but in T-SQL, you can also place it in brackets ([]). Look at these examples for the name of a table in T-SQL:

CREATE TABLE dbo.test.“first name” ( Id INT, Name VARCHAR(100));
CREATE TABLE dbo.test.[first name]  ( Id INT, Name VARCHAR(100));

Only the first delimiter (the quotation marks) for the special name is also part of the SQL standard.

What Is Different in a SELECT Statement?#2 Returning Values

The SQL standard does not have a syntax for a query returning values or values coming from expressions without referring to any columns of a table, but MS SQL Server does allow for this type of expression. How? You can use a SELECT statement alone with an expression or with other values not coming from columns of the table. In T-SQL, it looks like the example below:

SELECT 12/6 ;

In this expression, we don’t need a table to evaluate 12 divided by 6, therefore, the FROM statement and the name of the table can be omitted.

#3 Limiting Records in a Result Set

In the SQL standard, you can limit the number of records in the results by using the syntax illustrated below:


T-SQL implements this syntax in a different way. The example below shows the MS SQL Server syntax:


As you notice, this uses an ORDER BY clause. Another way to select rows, but without ORDER BY, is by using the TOP clause in T-SQL:

#4 Automatically Generating Values

The SQL standard enables you to create columns with automatically generated values. The syntax to do this is shown below:


In T-SQL we can also automatically generate values, but in this way:

#5 Math Functions

Several common mathematical functions are part of the SQL standard. One of these math functions is CEIL(x), which we don’t find in T-SQL. Instead, T-SQL provides the following non-standard functions: SIGN(x), ROUND(x,[,d]) to round decimal value x to the number of decimal positions, TRUNC(x) for truncating to given number of decimal places, LOG(x) to return the natural logarithm for a value x, and RANDOM() to generate random numbers. The highest or lowest number in a list in the SQL standard is returned by MAX(list) and MIN(list) functions, but in Transact-SQL, you use the GREATEST(list) and LEAST(list) functions.

T-SQL function ROUND:


#6 Aggregate Functions

We find another syntax difference with the aggregate functions. The functions COUNT, SUM, and AVG all take an argument related to a count. T-SQL allows the use of DISTINCT before these argument values so that rows are counted only if the values are different from other rows. The SQL standard doesn't allow for the use of DISTINCT in these functions.

Standard SQL:



But in T-SQL we don’t find a population covariance function: COVAR_POP(x,y), which is defined in the SQL standard.

#7 Retrieving Parts of Dates and Times

Most relational database systems deliver many functions to operate on dates and times.

In standard SQL, the EXTRACT(YEAR FROM x) function and similar functions to select parts of dates are different from the T-SQL functions like YEAR(x) or DATEPART(year, x).

There is also a difference in getting the current date and time. Standard SQL allows you to get the current date with the CURRENT_DATE function, but in MS SQL Server, there is not a similar function, so we have to use the GETDATE function as an argument in the CAST function to convert to a DATE data type.

#8 Operating on Strings

Using functions to operate on strings is also different between the SQL standard and T-SQL. The main difference is found in removing trailing and leading spaces from a string. In standard SQL, there is the TRIM function, but in T-SQL, there are several related functions: TRIM (removing trailing and leading spaces), LTRIM (removing leading spaces), and RTRIM (removing trailing spaces).

Another very-often-used string function is SUBSTRING.

The standard SQL syntax for the SUBSTRING function looks like:

SUBSTRING(str FROM start [FOR len])

but in T-SQL, the syntax of this function looks like:

SUBSTRING(str, start, length)

There are reasons sometimes to add values coming from other columns and/or additional strings. Standard SQL enables the following syntax to do this:

As you can see, this syntax makes use of the || operator to add one string to another.

But the equivalent operator in T-SQL is the plus sign character. Look at this example:

SELECT col1 + col2  FROM tab;

In SQL Server, we also have the possibility to use the CONCAT function concatenates a list of strings:

SELECT CONCAT(col1, str1, col2, ...)  FROM tab;

We can also repeat one character several times. Standard SQL defines the function REPEAT(str, n) to do this. Transact-SQL provides the REPLICATE function. For example:


where x indicates how many times to repeat the string or character.

#9 Inequality Operator

During filtering records in a SELECT statement, sometimes we have to use an inequality operator. Standard SQL defines <> as this operator, while T-SQL allows for both the standard operator and the != operator:

SELECT col3 FROM tab WHERE col1 != col2;
#10 ISNULL Function

In T-SQL, we have the ability to replace NULL values coming from a column using the ISNULL function. This is a function that is specific to T-SQL and is not in the SQL standard.

Which Parts of DML Syntax Are Different?

In T-SQL, the basic syntax of DELETE, UPDATE, and INSERT queries is the same as the SQL standard, but differences appear in more advanced queries. Let’s look at them.

#11 OUTPUT Keyword

The OUTPUT keyword occurs in DELETE, UPDATE, and INSERT statements. It is not defined in standard SQL.

Using T-SQL, we can see extra information returned by a query. It returns both old and new values in UPDATE or the values added using INSERT or deleted using DELETE. To see this information, we have to use prefixes in INSERT, UPDATE, and DELETE.

UPDATE tab SET col='new value'
OUTPUT Deleted.col, Inserted.col;

We see the result of changing records with the previous and new values in an updated column. The SQL standard does not support this feature.

#12 Syntax for INSERT INTO ... SELECT

Another structure of an INSERT query is INSERT INTO … SELECT. T-SQL allows you to insert data from another table into a destination table. Look at this query:

INSERT INTO tab SELECT col1,col2,... FROM tab_source;

It is not a standard feature but a feature characteristic of SQL Server.

#13 FROM Clause in DELETE and UPDATE

SQL Server provides extended syntax of the UPDATE and DELETE with FROM clauses. You can use DELETE with FROM to use the rows from one table to remove corresponding rows in another table by referring to a primary key and a foreign key. Similarly, you can use UPDATE with FROM update rows from one table by referring to the rows of another table using common values (primary key in one table and foreign key in second, e.g. the same city name). Here is an example:

FROM Author
WHERE Author.Id=Book.AuthorId AND Author.Name IS NULL;

SET Book.Price=Book.Price*0.2
FROM Author
WHERE Book.AuthorId=Author.Id AND Author.Id=12;

The SQL standard doesn’t provide this syntax.


You can also use INSERT, UPDATE, and DELETE using JOIN to connect to another table. An example of this is:

DELETE ItemOrder FROM ItemOrder
JOIN Item ON ItemOrder.ItemId=Item.Id
WHERE YEAR(Item.DeliveredDate) <= 2017;

This feature is not in the SQL standard.


This article does not cover all the issues about syntax differences between the SQL standard and T-SQL using the MS SQL Server system. However, this guide helps point out some basic features characteristic only of Transact-SQL and what SQL standard syntax isn’t implemented by MS SQL Server.

Thanks for reading. If you liked this post, share it with all of your programming buddies!

Originally published on

SQL Full Course - SQL Tutorial For Beginners - MySQL Essentials - SQL Training

SQL Full Course - SQL Tutorial For Beginners - MySQL Essentials - SQL Training

This SQL Full Course video will cover all the topics of SQL starting from scratch. This video is great for beginners who want to learn SQL and for advanced people to brush up their skills.

Thanks for watching

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Learn More

The Complete SQL Bootcamp

The Ultimate MySQL Bootcamp: Go from SQL Beginner to Expert

The Complete Oracle SQL Certification Course

MySQL Databases With Python Tutorial

SQL vs NoSQL or MySQL vs MongoDB

MySQL Database Tutorial for Beginners to Advanced Part 1/2

An Introduction to Queries in MySQL

How To Troubleshoot MySQL Queries?

SQL with MySQL - Complete Tutorial for Beginners

Build a Basic CRUD App with PHP and MySQL

What is SQL

SQL full form is Structured Query Language. SQL is a domain-specific language. It is used in application development language to enable a programmer to work with the data. The data is stored in a relational database. To manage this data, we have relational database management systems like SQL Server, MySQL, MS Access, etc. that use SQL as a standard database language.

This introduction to SQL tutorial will give you a quick overview on:

What Is Data?

Data is distinct pieces of information, which can be facts, figures, or details that are stored in or used by a computer.

What Is Data

What Is a Database?

A database is a well-organized collection of data that is stored in an electronic format. To be more specific, a SQL database is an electronic system that allows to easily access, manipulate, and update the data.

What Is a Database

What Is a Database Management System?

Consider a School SQL Database which has a record of the present students and the previously studied students in the Student Details table. Similarly, it may contain Faculty Details, Management Details, Staff Details , and many more depending on the school’s requirement. As the data is in huge amount, to manage it we need a database management system.

Watch this Introduction to SQL and SQL Commands video:

Database Management System Almost all modern databases are managed by a Database Management System (DBMS). Basically, it is a system software used for creating data in a systematic way and managing databases. 

How Does SQL Database manage Data?

DBMS provides, for both users and programmers, a fundamental way to create, retrieve, update, and manage data.

Consider a scenario where a student XYZ wants to change his address. The DBMS searches the details of XYZ in the table ‘Student Details’ from the database ‘School Database’ and displays it for the user, and then the user edits it.

Now, we have a clear picture of a database and its managing system. Let’s move on.

Types of Database Architecture

We have two types of database architecture:

File Server Architecture

File Server Architecture In the file server architecture, files are located on the local system. It is useful for sharing information across a network. The client sends a request for a file over the network, and the file server forwards the file to the client. This is considered to be the most primitive type of data service used for exchanging information over a network. The file server provides access to remote server processors too. Here is an example to understand the implementation of the file server.

File Server Architecture 2Suppose, you have an Excel file and one of your friends requests you to send that file for some information. So, you send a copy to your friend. Now when you make any changes in your original Excel file, those changes would not be reflected in the file which is with your friend.

In this example, you are a file server and your friend is the device requesting for the information. We can conclude on the file server architecture by quoting that the server acts as a sorting device and only one person at a time can have access to it.

Client Server Architecture

In the client server architecture, the database is the server and any application that uses the data is a client. Here is an example to explain the working of this server. Consider, three OLE DB components or client systems accessing the database at the same time. The systems have logged into the IRCTC website to know the number of trains running from X destination to Y destination.

Client Server Architecture

The client system sends the request to the network server. The network server sends the same request to the database and the final result is sent to the client system. This process is conducted when one system sends the request. But in real time, there will be n number of systems and there can be multiple requests that are sent at a time for the same data. Database server will have to process all the requests simultaneously and send back the requested data to the client systems.

What Is SQL?

As discussed before SQL full form is a structured query language, it helps you to communicate with the database by commands. Here are some of the features of SQL Database:

It allows users to extract data from relational database.

It allows to create database and tables.

It allows updating, inserting, deleting and altering database and tables.

It provide security and allow to set a permission.

Allows people to handle data in new ways.

A Brief Explanation on SQL

Types of SQL Commands

SQL commands are traditionally divided into four categories:

Data Query Language (DQL Commands in SQL)

Data Definition Language (DDL Commands in SQL)

Data Manipulation Language (DML Commands in SQL)

Data Control Language (DCL Commands in SQL)

Categories of SQL Commands

Data Query Language (DQL Commands in SQL)

Data Query Language comprises only one command ‘select.’ This command can be accompanied by many other clauses to compose queries.

Data Definition Language (DDL Commands in SQL)

Data Definition Language is power for SQL, which allows a user to create and restructure database objects. The basic DDL commands in SQL are Create Tables, Alter Tables, and Drop Tables.

| CREATE TABLE | It creates a new table |

| DROP TABLE | It deletes the ENTIRE table. |

| ALTER TABLE | Modifies the existing table |

Data Manipulation Language (DML Commands in SQL)

Data Manipulation Language is used to manipulate data within the tables. The basic DML commands in SQL are Insert, Update and Delete.

| SELECT | Retrieve information from database |

| INSERT | Add new information to a database |

| UPDATE | Modifies the information currently stored in a database |

| DELETE | Delete information from the database |

Data Control Access Language (DCL Commands in SQL)

And finally, we have Data Control Access which allows the user to control access to data within the database. These DCA commands are generally used to control the distribution of privileges among users and create objects related to user access. The basic DCL commands in SQL are Grant and Revoke.

This bring us to the end of Introduction to SQL. Here we have learnt what is SQL, SQL full form, SQL Database, all basic SQL commands and its types – DDL, DCL, DML and DQL with examples.