question

Matt Whitfield avatar image
Matt Whitfield asked

What is the difference between an Expression and a Statement?

Seeder Question:

I have seen people talk about expressions and statements in T-SQL, but I don't understand the difference. Can the two be used interchangeably?

t-sqlseeder-questionterminology
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image
Kev Riley answered

An expression can be evaluated, and as such returns a single value. It is only one possible part of a statement.

A statement is a collection of elements such as identifiers, reserved keywords, data types, functions, expressions, operators and comments, to make the smallest possible unit of code. A statement can be executed.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Bob Hovious avatar image
Bob Hovious answered

An expression returns a single value. A statement defines control of flow, data manipulation, or data definition operations. Column names, variables, constants, functions and formulas using any of the preceding are all expressions. Certain subqueries that return only a single value may also be considered expressions.

It's worth mentioning that one of the difficulties newcomers to SQL experience when working with a CASE expression is that they confuse it with an IF statement. The key to understanding which is which is the realization that CASE, unlike IF, does no flow control: it simply returns a single value. CASE (or ISNULL or COALESCE) is really more like a function with embedded IF logic.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sagar Bhargava avatar image
Sagar Bhargava answered

What I roughly know about this is that an expression is like a phrase; a statement is like a complete sentence. A statement can include one or more expressions to carry out a computation.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Scot Hauder 4 avatar image
Scot Hauder 4 answered

An expression is the scornful look on her face when you make a statement about her ill-fitting dress

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.