Note: I’m posting 2 days together because I missed posting day before yesterday.
Today, we’ll be looking at AND, OR, NOT And ORDER BY statements in SQL which are again pretty easy to implement. The good thing about SQL is that it is simple English as
said in one of her posts.So, without further ado, lets get cracking, opening SSMS.
Use Of AND, OR, And NOT In SQL
The statements AND, OR and NOT as obvious by their meaning allow us to provide further conditions while filtering our data.
I’m going to be using the same video games dataset from Maven Analytics and let’s say I want to see which of the games were launched on PS4 and their publisher is Activision.
We can see that only 40 rows out of 64k have publisher as Activision and console as PS4.
Now what if we wanted to see games by Activision as well as Rockstar Games?
Right, let’s customize our query a bit.
So we can see that if we see games by Rockstar as well, our records go to 220 (right bottom corner).
Now what if I wanted to see consoles other than PS4? Well I wouldn’t add all the consoles again and again with an AND in between, I mean I could do that but why not use NOT?
Using NOT I only have to declare that my console should not be PS4.
So here’s our query
Note that I have added a bracket after and and closed it after I declare the third condition. This is because of operator precedence in SQL. What happens if I don't add brackets? It will only use my NOT condition on Activision and ignore Rockstar Games.
Its pretty east and simple, I hope you understood it. However, if you don’t, I’ll be available for guidance in the comments section.
Lets move on.
Use Of ORDER BY In SQL
ORDER BY allows our query results to be sorted into ascending or descending order of the column we specify in our query. It wont make much sense for beginners until you see what it does and practice it yourself.
So, let’s begin.
I have used descending order to order my results on the basis of titles. If I had used order by title
it would have sorted in ascending order by default.
I hope you got the concept of order by and soon we’ll start using more complex queries, however enjoy while its simple. Here’s the complete query I used
select title, console from dbo.['Video Game Sales$']
where not console = 'PS4' and (publisher = 'Activision' or publisher = 'Rockstar Games')
order by title desc
Exercise
Here’s what I need you to do today:
Load a dataset/table in your IDE
Use AND in your query
Use OR in the same query
Use NOT in the same query
Use ORDER BY in the same query
Comment your query and what it did.
I have given you a reference query above, all you need to do is put in your columns and decide what you want to see. If you need personal guidance, I would recommend getting the paid subscription and I’ll be helping you out personally in DMs.
I am a BI Engineer working at a Data And AI Firm. I’m trying to help newbies in Data land their first data job and impact as many fellow data professionals as I can. Here’s my contact info:
ajmal7809@gmail.com
bideveloper_ (Discord)
I put in a great effort to provide you with guidance. I put out free content for your learning. If you’re willing you can buy me a coffee but its totally up to you, no compulsion.