Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Software ==> | SQL (203 exercises) |
Topic ==> | Setting criteria using WHERE (12 exercises) |
Level ==> | Harder than average |
Subject ==> | SQL training |
This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.
This will generate the database that you'll need to use in order to do this exercise (note that the database and script are only to be used for exercises published on this website, and may not be reused or distributed in any form without the prior written permission of Wise Owl).
First show a list of all events which might have something to do water. The Wise Owl interpretation of this is that one or more of the following is true:
This should return 11 rows. Now add a criterion to show only those events which happened since 1970 (you may need to use parentheses to get this to give the correct answer).
Here's what you should see as a result:
The final results from running this complicated query.
Save this as Somewhat contrived, then close it down.
You can find other training resources for the subject of this exercise here:
From: | Happy_Annie |
When: | 23 Feb 24 at 03:00 |
SELECT EventName, EventDetails, FORMAT(EventDate, 'yyyy-MM-dd')
FROM tblEvent
WHERE CountryID IN (8, 22, 30, 35)
OR EventDetails LIKE '%Water%'
OR CategoryID = 4
AND YEAR(EventDate) > 1970
ORDER BY EventDate;
The result shows 13 rows. I have no idea which part is wrong. Please help.
From: | Andy B |
When: | 23 Feb 24 at 09:13 |
You're showing all the events made where the year is after 1970, rather than after the first day of 1970. It should be >= 1970..
From: | niflheimis |
When: | 16 Feb 22 at 20:44 |
Hi,
Why is the parenthesis necessary? I did notice that without it the "AND EventDate >= '19700101'" doesn't work, but I don't understand why.
I would appreciate an explanation :)
From: | Andy B |
When: | 17 Feb 22 at 12:06 |
Suppose you want to pick out all the pets from a pet shop which are fluffy or scaly and which have 4 legs. If your criteria reads:
Fluffy or Scaly and 4 legs
This might be interpreted as giving all pets which are either fluffy or which are 4-legged and scaly. So whereas you wanted fluffy or scaly quadrupeds, your query would give you all fluffy animals, regardless of their number of legs. Putting brackets in makes the query do what you want:
(Fluffy or Scaly) and 4 legs
From: | ShukiMolk |
When: | 11 Apr 20 at 02:53 |
Actually, there is a small bug with previous answers. When using this criterion:
(EventDetails) like '% water %
we get indeed events that have the word 'water' in them (with space before and after the word 'water'), but we leave out events that begins or ends with the word 'water'. True, there are none in the database, but if we add them, we could see that they are left out.
Try adding these events:
INSERT INTO [tblEvent]
VALUES ('Event that BEGINS with water', 'Water falling from the sky', GETDATE(), 1, 1),
('Event that ENDS with water', 'There is no more water', GETDATE(), 1, 1)
There should be 7 rows now, shouldn't there?
In order to "capture" also events that start or end with the word 'water' we should add these conditions:
OR [EventDetails] LIKE 'water %'
OR [EventDetails] LIKE '% water'
Hope this helps.
P.S. In order to remove these 2 events that we added, execute the following script:
DELETE FROM [tblEvent]
WHERE
[EventDetails] LIKE 'Water falling from the sky'
OR
[EventDetails] LIKE 'There is no more water'
From: | Priyanshu |
When: | 22 Dec 19 at 20:36 |
SELECT EventName, EventDetails, EventDate
FROM tblEvent
WHERE (CategoryID = 4 OR CountryID IN (8,22,30,35) OR EventDetails LIKE '% WATER %')
AND DATEPART(YEAR, EventDate) > 1970;
From: | rasheedshd |
When: | 24 Sep 18 at 21:17 |
I am not able to rin this, can anybody help.
select EventName, eventdetails, EventDate
from tblEvent
where CountryID in (8,22,30,35)
or eventdetails like '%water%' or
CategoryID = 4
and eventdate > 1970-01-01
I am getting this error message:
Operand type clash: date is incompatible with int
From: | Andrew G |
When: | 25 Sep 18 at 07:31 |
Hi, try writing the date within a set of single quotes, like this:
'1970-01-01'
I hope that helps!
From: | Kareem44 |
When: | 07 Sep 17 at 10:03 |
cannot solve it
select EventName, EventDetails, EventDate from tblEvent where (CountryID = 8 or CountryID = 22 or CountryID = 30 or CountryID = 35) or EventDetails = '%water%' or CategoryID = 4 and year(EventDate) >= 1970
From: | Andy B |
When: | 07 Sep 17 at 16:47 |
You need to put LIKE and not =:
EventDetails LIKE '%water%'
This will show rows where the event details match the pattern contained in the text string.
From: | eric |
When: | 05 Nov 17 at 07:27 |
Hi
Correct answer is:
EventDetails like '% water %'
with spaces around. You are looking for the word water , not any text with water in it.
Full query:
select * from tblEvent
where (CountryID in(8,22,30,35) or
EventDetails like '% water %' or
CategoryID = 4) and EventDate > '1970-01-01'
From: | piresmdb |
When: | 18 Jul 17 at 09:55 |
Combine criteria to show possibly watery post-1970 events!
Thee are only 4 events under the CategoryID 4. How come you have five in your solution?
From: | Andrew G |
When: | 30 Jul 17 at 10:02 |
The clue is in the question!
"one OR more of the following is true"
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.