- Simple Queries (4)
- Setting criteria using WHERE (5)
- Calculations (7)
- Calculations using dates (4)
- Basic joins (8)
- More exotic joins (2)
- Aggregation and grouping (8)
- Views (5)
- Subqueries (5)
- Stored procedures (5)
- Variables (8)
- Parameters and return values (11)
- Testing conditions (1)
- Looping (3)
- Scalar functions (6)
- Transactions (5)
- Creating tables (5)
- Temporary tables and table variables (9)
- Table-valued functions (6)
- Derived tables and CTEs (13)
- Dynamic SQL (4)
- Pivots (2)
- Triggers (2)
- Archived (70)
SQL | Setting criteria using WHERE exercise | Combine criteria to show possibly watery post-1970 events!
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.
- Go into SQL Server Management Studio;
- Open the SQL file you've just unzipped (you can press CTRL + O to do this); then
- Execute this script.
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).
You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
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:
- They take place in one of the island countries (8, 22, 30 and 35, corresponding to Japan, the Marshall Islands, Cuba and Sri Lanka respectively)
- Their EventDetails column contains the word Water (not the text Water, but the word)
- Their category is number 4 (Natural World)
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.
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]
[EventDetails] LIKE 'Water falling from the sky'
[EventDetails] LIKE 'There is no more water'
SELECT EventName, EventDetails, EventDate
WHERE (CategoryID = 4 OR CountryID IN (8,22,30,35) OR EventDetails LIKE '% WATER %')
AND DATEPART(YEAR, EventDate) > 1970;
I am not able to rin this, can anybody help.
select EventName, eventdetails, EventDate
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
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
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.
Correct answer is:
EventDetails like '% water %'
with spaces around. You are looking for the word water , not any text with water in it.
select * from tblEvent
where (CountryID in(8,22,30,35) or
EventDetails like '% water %' or
CategoryID = 4) and EventDate > '1970-01-01'
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?