keyword – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Wed, 17 Aug 2022 07:21:31 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg keyword – Programmerbay https://programmerbay.com 32 32 Difference Between In and Between Operator In SQL https://programmerbay.com/sql-between-and-in-keywords/ https://programmerbay.com/sql-between-and-in-keywords/#respond Wed, 04 May 2022 05:47:00 +0000 http://programmerbay.com/?p=3103

In this article, we’ll be discussing the comparison between IN and BETWEEN Operator in SQL.

However, the main difference is, BETWEEN Operator fetches tuples that lies within the specified range, whereas, IN Operator is used as an alternative to multiple ORs.

Difference between In and Between Operator in SQL Tabular form

BasisBetween OperatorIN Operator
DefinitionIt is used to get a set of records ranging between two valuesIt is used to get records whose column's value matches with given set of values
SyntaxSELECT * FROM tableName WHERE Column BETWEEN range1 AND range2;SELECT * FROM tableName WHERE name IN(value,value1,value2.....);
WorkingReturn the records whose column's value lies in between the given rangeCompares the given column's value, returns that record if a match exists in the set of values

Between Operator

This Keyword is used to get a set of records which falls within a given range. In other words, It is equivalent to <= lower_bound and >=upper_bound.

It helps to  shrink a long statement (1<=id AND id<=5) to  Id BETWEEN 1 AND 5. We can use it with numeric values as well as alphabates.

Syntax:
SELECT * FROM tableName WHERE Column BETWEEN range1 AND range2;

For Example, there is a table coder and the table looks like:

coder table

Suppose, We require all the records whose ID ranges from 1 to 3.

SELECT * FROM coder WHERE id BETWEEN 1 AND 3;

between example

IN Operator

Instead of using, multiple ORs, we have IN keyword to select only those records whose column’s value matches with given values.

Like BETWEEN keyword, It also reduces the length of statements.

Syntax:

SELECT * FROM tableName WHERE name IN(value,value1,value2…..);

Note: if it isn’t numeric then each value must be enclosed within quotes.

It would return only those records whose targeted column consists values that matches with mentioned  set of values.

For Example:
This the table from which I wish to select records of  ‘john’ and ‘micky’ from coder table

coder table

SELECT * FROM coder WHERE first_name IN (‘john’,’micky’);

in output

]]>
https://programmerbay.com/sql-between-and-in-keywords/feed/ 0
Check a Column Contains NULL or Empty using WHERE Clause in SQL https://programmerbay.com/column-contains-null-or-empty-using-where-clause-in-sql/ https://programmerbay.com/column-contains-null-or-empty-using-where-clause-in-sql/#respond Sun, 29 Apr 2018 11:12:00 +0000 http://programmerbay.com/?p=3105

What is NULL?

NULL represents an undefined. It cannot be counted as an empty string (‘ ‘) or zero(0), it’s way different from these. 
Sometimes, It’s not necessary to fill up each and every field with data/information, whenever be a record is inserted or updated. If a record is inserted only with targeted fields only (leaving other fields ), then automatically that particular field/column would get NULL value. 

It can be avoided by using NOT NULL constraint that would prevent from entering  NULL value.
Moreover, we cannot compare two NULL values with one other using operators. 

How to know which field contain NULL value ?

For this, SQL provides us two operators ,  ‘IS NULL’ and ‘IS NOT NULL’ to know whether a particular column consists a NULL Value or not.

IS NULL

It will return all the records which has NULL values in its fields. 
Syntax: Select Col1,Col2,… FROM table WHERE Col  IS NULL;

IS NOT NULL

It will return all the records which hasn’t NULL values in its fields.
Syntax: Select Col1,Col2,… FROM table WHERE Col  IS NOT NULL;

Check a column Contains NULL or Empty String using WHERE Clause in SQL?

Suppose there is a table named ‘customer’ and we want to find all the records of customers having “lastname” column containing null or empty string in the table.
demo table
Query :
SELECT * FROM   customers WHERE  customer_lastname IS NULL OR customer_lastname = ''

Output:

output

About Where Clause

In SQL, WHERE Clause is used to specify a condition and fetching the data from the given table. whether it is single table or by joining of multiple ones. If the condition is satisfied, the result comes out with specific values. So we should use the WHERE clause to filter the records and extract the results that fulfilling specific condition.
In SQL the WHERE clause not only used with the SELECT statements but also with the UPDATE, DELETE statements etc. for getting small clues, some examples/syntax are here to get through but for gaining more about it, we would examine these in next subsequent posts.

Syntax:

SELECT column1, column2, columnN
FROM table_name
WHERE condition;

For Example:

From an Extract of the table data which shows the List of Customers of different Countries and we filter out for some let’s say “India”. So, the Problems become is “List of Customers in India” and there columns contains ID, Name, Product, City, and Country. let’s find out how it filters:


1. SELECT ID, Name, Product, City, Country
2. FROM Customers
3. WHERE Country = ‘India’

Output:

table



There you go “Result” in before your eyes.

There are many Operators that can be used in WHERE clause as:
< > not equal
= equal
> grater than
< less than
LIKE, BETWEEN & IN are also the Operators which can be used in WHERE clause for searching a pattern, Between an inclusive range & to specify the multiple values for a column respectively.

 

]]>
https://programmerbay.com/column-contains-null-or-empty-using-where-clause-in-sql/feed/ 0