Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. SQL Server
  3. Question
SQL Server

How do I use where Max SQL?

Asked by Ranjana Admin Feb 7, 2023 965 views 1 answer
Share

About this question

I am attempting to use MAX() in my WHERE clause but I am getting an error of Msg 4145, Level 15, State 1, Line 10

An expression of non-boolean type specified in a context where a condition is expected, near ')'.

This is the output I expect from working syntax

Blue - 3
green - 0
orange - 1
pink - 1
black - 0
white - 2
Here is sample table structure and the query I tried - how can I successfully use MAX() in the WHERE clause?
Declare @Hold Table 
(
  id int NOT NULL IDENTITY (1,1), 
  name varchar(500), 
  logoncount int
)
Insert Into @HOld (name, logoncount) Values
('blue', 1),('blue', 2), ('blue', 3)
,('green', NULL), ('orange', 1), ('pink', 1)
,('black', NULL), ('white', 1), ('white', 2)
Select 
*
FROM @Hold
WHERE logoncount = (select max(logoncount) from @Hold)
sql-server

Your answer

1 Answer

More SQL Server discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest SQL Server Blogs

Guides, tips and career advice on SQL Server from JanBask experts.