Skip to main content

SQL SERVER – What Does WHERE 1=1 Mean in the Code?

One of the blog followers asked this question “The code written by my colleague has WHERE 1=1. What does it mean?”

Well. You have seen people using it when they dynamically construct WHERE condition based on the input values.

Let us create this dataset

CREATE TABLE #t (emp_name VARCHAR(100), experience INT, specialist VARCHAR(100))
INSERT INTO #t
SELECT 'Pinal',12,'SQL' UNION ALL
SELECT 'John',10,'JAVA' UNION ALL
SELECT 'Sudhan',3,'SQL'

Suppose you want to use two parameters and pass values for them in such a way that if the value is null, you need to skip the comparison
Check the following code

DECLARE @experience INT, @specialist VARCHAR(100), @sql VARCHAR(100)
SET @sql=' where 1=1'
SET @sql = @sql+CASE WHEN @experience IS NULL THEN '' ELSE ' and experience='''+CAST(@experience AS VARCHAR(100))+'''' END
SET @sql = @sql+CASE WHEN @specialist IS NULL THEN '' ELSE ' and specialist='''+CAST(@specialist AS VARCHAR(100))+'''' END
SELECT @sql AS value

value
---------------
where 1=1
Because no values are passed the column valu comparions are skipped and you have 1=1 in the WHERE condition

DECLARE @experience INT, @specialist VARCHAR(100), @sql VARCHAR(100)
SET @sql=' where 1=1'
SET @specialist='SQL'
SET @sql = @sql+CASE WHEN @experience IS NULL THEN '' ELSE ' and experience='''+CAST(@experience AS VARCHAR(100))+'''' END
SET @sql = @sql+CASE WHEN @specialist IS NULL THEN '' ELSE ' and specialist='''+CAST(@specialist AS VARCHAR(100))+'''' END
SELECT @sql AS value

If you execute the above , the result is

value
-------------------------------
where 1=1 and specialist='SQL'
It is because the value for column specialist is passed and it is included in the WHERE clause. This way to build WHERE clause dynamically you may need to use 1=1 as the first condition.

Now you can execute the statement using

EXEC(' select * from #t'+@sql)

Comments

Popular posts from this blog

Basic SEO Explained: How Search Engines Really Work

Basic SEO Explained: How Search Engines Really Work Search Engine Optimization (SEO) is the process of improving your website so that it appears higher in search engine results pages (SERPs). SEO helps users find your content organically without paid ads and brings long-term traffic. If you are new to SEO, you should first read our complete on-page SEO guide for beginners to understand the basics clearly. How Search Engines Work (Step-by-Step) Search engines work in three main stages: crawling, indexing, and ranking. Understanding these steps is essential for building SEO-friendly websites. 1. Crawling Crawling is the process where search engines use bots (such as Googlebot) to discover new and updated pages. These bots follow links from one page to another across the web. Internal links help crawlers find your content faster. Learn more in our internal linking strategy guide . 2. Indexing After crawling, search engines analyze and store your pages in...

What Is Web Hosting? A Beginner’s Complete Guide

What Is Web Hosting? A Beginner’s Complete Guide Web hosting is an essential service that allows websites to be accessible on the internet. Without web hosting, a website cannot be viewed online. What Is Web Hosting? Web hosting is a service that stores your website files on a server and delivers them to users when they visit your website. How Web Hosting Works When a user types your website address, the browser sends a request to the hosting server, which then displays your website files. Types of Web Hosting Shared Hosting: Affordable and beginner-friendly VPS Hosting: Better performance and control Dedicated Hosting: Full server access Cloud Hosting: Flexible and scalable Why Web Hosting Is Important Good hosting improves website speed, security, and uptime. Conclusion Web hosting is the foundation of every website. Beginners should start with shared hosting and upgrade as their site grows. Frequently Asked Questions Is web hosting necessary? Ye...

How to Start a Blog Step by Step (Beginner Friendly)

How to Start a Blog Step by Step (Beginner Friendly) Starting a blog is an excellent way to share knowledge and build an online presence. With the right approach, anyone can start blogging. Choose a Blog Topic Select a topic you enjoy and can write about consistently. Select a Blogging Platform Popular platforms include Blogger and WordPress. Blogger is free and easy for beginners. Choose a Domain Name Your domain name should be simple, memorable, and related to your blog topic. Design Your Blog Use a clean, fast-loading theme that works well on mobile devices. Create Quality Content Focus on original, helpful, and easy-to-read content. Conclusion Blogging requires patience and consistency. With quality content, your blog can grow over time. Frequently Asked Questions Can I start a blog for free? Yes, platforms like Blogger allow free blogging.