SQL filtering with queries

Photo by Dziana Hasanbekava from Pexels

Before implementing this article, have a look at database, keys and sql command by going through the below article

Understanding database, SQL and its commands

This article is a continuation of the above article. So first go through the above article so that clear understanding of select and show commands can be achieved. For SQL filtering with queries, select command will be used very often. Filtering helps to provide only the desired results. In this article we will implement where clause, between, in, and, or operator to apply filters to the results.

WHERE CLAUSE

It will retrieve those results which are based on condition applied using where clause.

Let us see the syntax 

select * from table where condition

This statement selects all records which fulfils the particular condition which one can mention in the where clause. Have a look at the below screenshot

WHERE CLAUSE
WHERE CLAUSE

BETWEEN OPERATOR

It will retrieve those results which are based on lower and upper bounds set by the between operator. One can choose the bound accordingly. Remember that both the upper and lower bound is included during retrieval. It is used with the where clause. 

BETWEEN OPERATOR
BETWEEN OPERATOR

CONJUNCTIVE OPERATORS

Operators  (or and) are called conjunctive operators. They can be used together in the query to filter the results based on the condition applied. 

CONJUNCTIVE OPERATORS
CONJUNCTIVE OPERATORS

IN OPERATOR

It is used when you want to compare a column field for more than one value. You can achieve the same result using OR operator but the length of query will increase.

IN OPERATOR
          IN OPERATOR

NOT IN OPERATOR

It is used when one needs to exclude list of values from a column fields during data retrieval.

NOT IN OPERATOR
         NOT IN OPERATOR

Point to note: When one is implementing sub query inside a query then enclose the subquery using parentheses.

CONCAT FUNCTION

It is used to concat two or more text values and returns the concatenated string.

CONCAT FUNCTION
CONCAT FUNCTION

So, from this article one can learn how to filter data using SQL queries and how to apply subqueries in the query taking into consideration that parenthesis is to be applied for a sub query. In the next coming post joins will be discussed. Read the basics of SQL and its commands from below

Understanding Database, SQL and it’s commands

Continue Reading

You may also like

Understanding Database, SQL and it’s commands

Photo by Jan Antonin Kolar on Unsplash

Database is a collection of data which is organized efficiently in order to access, retrieve and update data quickly. Data is stored in a structured format in a table in the form of rows and columns. Table has specified number of columns or fields but can have any number of rows. In this post we are going to learn about types of keys, structured query language (SQL) and its commands.

Types of Keys

  • Primary Key: Primary key is a field which cannot be null as well as unique for each row. There is a single primary key for each table and a single table cannot have two primary keys.
  • Super Key: It is mixture of different fields such that one of the field is uniquely identifies i.e. one of the key must be unique for all the records or rows in the table. It can be a single field or group of keys.
  • Candidate Key: It is a category of super key such that all fields that makes it must be unique for all records in the database. It can be single or group of keys.
  • Foreign Key: If the primary key of one table is used in another table, then in another table it is termed as foreign key. Foreign key can be the same for two records and also can be null.   

SQL

SQL is an acronym for Structured Query Language. It is used to access as well as manipulate databases. Using SQL one can create database, tables, add new records to the table inside the database or remove them. For the users to perform these action efficiently one needs tools or interface which is provided by database systems. Different database systems based on SQL language are Microsoft SQL, MySQL, PostgreSQL and many more. 

Basic SQL commands

  • SHOW : It shows information contained like if command show databases; is executed inside MySQL server, it will list databases managed by the server. Have a look at the below screenshot
show databases
show databases

Another show command is show tables. To show tables of the database first select database using the command use *database_name* and then type show tables. Suppose, I have selected information_schema database which is by default listed in MySQL Server and typed the below command

show tables;

Output for such is as below

show tables
show tables

Another show command is show columns. To execute this command one needs to select the table from the database. Suppose I would like to see the field’s description of the table CHARACTER_SETS from information_schema database. I will execute the below command

show columns from CHARACTER_SETS;

Following is the output of the above execution

show columns from table
show columns from table
  • SELECT : It selects the data from the database. For example, I need to get all the values associated with a particular field in a table, I will execute the below command

 select column_field from table;

Have a look at the below screenshot

Listing values associated with a column field using select query
                         Listing values associated with a column field using select query

Point to note is that SQL is case-insensitive. It does not matter whether you write in capital letters or small. But statements ends with a semicolon. White spaces and multiple lines are ignored in SQL. 

If one writes select * from table_name, it will fetch all the records from a table created inside the database. Have a look at below screenshot

select * from table_name
                              select * from table_name

select command can be used with distinct keyword which retrieves only unique records.  

Key thing to understand here is when multiple columns fields are used to retrieve information using select with distinct, then only values repeated in all selected multiple column fields would be ignored.

select can also be used with limit keyword which will limit the records to be returned. One can use offset with limit keyword. Offset means not taking into consideration the number of rows mentioned along with offset. For example if I say offset 3, it means that the first 3 rows are ignored and results would be displayed starting from row 4th.

select can also be used with order by keyword which sorts the result based on the selected column field. By default, sorting is done in the ascending order. 

Learn about filtering the records from the database using sql commands and operators.

SQL filtering with queries

Continue Reading

You may also like