One of the frequently asked questions is the use of if-else conditional structure in our SQL queries. One common feature of programming languages is the use of conditional statements like "if this, then that", or "if the shirt is blue, choose a white tie". It is possible to use this structure in SQL as well. Let’s delve into this topic with a few examples to better understand it.
First, let's create a user table: tblUsers (Users table)
CREATE TABLE tblUsers (
user_ID INT PRIMARY KEY NOT NULL IDENTITY(1,1),
first_name VARCHAR(50),
last_name VARCHAR(50),
gender VARCHAR(1),
username VARCHAR(50),
id_number VARCHAR(20),
birth_date DATE
)
To perform operations on the table, let's add a few records:
User Data
INSERT INTO tblUsers(first_name, last_name, gender, username, id_number, birth_date)
VALUES ('Serkan', 'TOGAL', 'M', 'ontedi', '12345678901', '01.01.1980')
INSERT INTO tblUsers(first_name, last_name, gender, username, id_number, birth_date)
VALUES ('Asmin Nisa', 'TOGAL', 'F', 'asmin', '12312312322', '01.01.2014')
INSERT INTO tblUsers(first_name, last_name, gender, username, id_number, birth_date)
VALUES ('Ahmed ?hsan', 'TOGAL', 'M', 'ahmed', '33344455566', '01.01.2016')
INSERT INTO tblUsers(first_name, last_name, gender, username, id_number, birth_date)
VALUES ('Yusuf Taha', 'FISTIK', 'M', 'yusuf', '55566677788', '01.02.2016')
Example 1
DECLARE @your_name VARCHAR(50) = 'Serkan'
IF @your_name IS NOT NULL
SELECT * FROM tblUsers WHERE first_name = @your_name
Example 1 Result
Example 2
DECLARE @your_name VARCHAR(50) = 'Ahmed ?hsan'
DECLARE @your_last_name VARCHAR(50) = 'TOGAL'
IF (@your_name IS NOT NULL) AND (@your_last_name IS NOT NULL)
SELECT * FROM tblUsers WHERE first_name = @your_name AND last_name = @your_last_name
Example 2 Result
Example 3
DECLARE @gender VARCHAR(1) = 'F'
IF @gender = 'F'
SELECT * FROM tblUsers WHERE gender = 'F'
Example 3 Result
In our final example, let's list people who were not born with no empty birth date and were born after January 1, 2015.
Example 4
DECLARE @birth_date DATE = '2015-01-01'
IF @birth_date IS NOT NULL
SELECT * FROM tblUsers WHERE birth_date > @birth_date
Source: Using If Else in SQL
Happy querying days!
First, let's create a user table: tblUsers (Users table)
CREATE TABLE tblUsers (
user_ID INT PRIMARY KEY NOT NULL IDENTITY(1,1),
first_name VARCHAR(50),
last_name VARCHAR(50),
gender VARCHAR(1),
username VARCHAR(50),
id_number VARCHAR(20),
birth_date DATE
)
To perform operations on the table, let's add a few records:
User Data
INSERT INTO tblUsers(first_name, last_name, gender, username, id_number, birth_date)
VALUES ('Serkan', 'TOGAL', 'M', 'ontedi', '12345678901', '01.01.1980')
INSERT INTO tblUsers(first_name, last_name, gender, username, id_number, birth_date)
VALUES ('Asmin Nisa', 'TOGAL', 'F', 'asmin', '12312312322', '01.01.2014')
INSERT INTO tblUsers(first_name, last_name, gender, username, id_number, birth_date)
VALUES ('Ahmed ?hsan', 'TOGAL', 'M', 'ahmed', '33344455566', '01.01.2016')
INSERT INTO tblUsers(first_name, last_name, gender, username, id_number, birth_date)
VALUES ('Yusuf Taha', 'FISTIK', 'M', 'yusuf', '55566677788', '01.02.2016')
Example 1
DECLARE @your_name VARCHAR(50) = 'Serkan'
IF @your_name IS NOT NULL
SELECT * FROM tblUsers WHERE first_name = @your_name
Example 1 Result
Example 2
DECLARE @your_name VARCHAR(50) = 'Ahmed ?hsan'
DECLARE @your_last_name VARCHAR(50) = 'TOGAL'
IF (@your_name IS NOT NULL) AND (@your_last_name IS NOT NULL)
SELECT * FROM tblUsers WHERE first_name = @your_name AND last_name = @your_last_name
Example 2 Result
Example 3
DECLARE @gender VARCHAR(1) = 'F'
IF @gender = 'F'
SELECT * FROM tblUsers WHERE gender = 'F'
Example 3 Result
In our final example, let's list people who were not born with no empty birth date and were born after January 1, 2015.
Example 4
DECLARE @birth_date DATE = '2015-01-01'
IF @birth_date IS NOT NULL
SELECT * FROM tblUsers WHERE birth_date > @birth_date
Source: Using If Else in SQL
Happy querying days!