query – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Mon, 04 Jul 2022 06:14:35 +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 query – Programmerbay https://programmerbay.com 32 32 How to Select Distinct Rows in SQL ? https://programmerbay.com/select-distinct-statement-to-get-only-unique-records-from-table/ https://programmerbay.com/select-distinct-statement-to-get-only-unique-records-from-table/#respond Fri, 16 Aug 2019 17:09:00 +0000 https://www.programmerbay.com/select-distinct-statement-to-get-only-unique-records-from-table/

The SELECT DISTINCT statement would let you get only unique information from a field that consists lots of duplicate data. In other words, a field that contains  lots of repeated values, to ignore those repeated values the SELECT DISTINCT statement is used.

It won’t allow the column’s data to repeat more than a single time and it gives filtered information with optimization as it doesn’t fetch entire duplicate information (take less memory).

Syntax:

SELECT DISTINCT feild1,feild2,.. FROM  Table_name;

For Example:

Suppose you have a table namely as user_details:

distinct question


and if you want to fetch only non repeated value of gender field, the command would be:

SELECT DISTINCT gender FROM user_details; 

Output:

distinct

 

]]>
https://programmerbay.com/select-distinct-statement-to-get-only-unique-records-from-table/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