Search

Friday, 20 December 2019

Introduction to SQL - DISTINCT Keyword


SQL DISTINCT Keyword
SQL DISTINCT returns unique records from a table in the SQL database. By default SQL queries return all rows, including duplicate records. The SQL DISTINCT keyword is used to eliminate duplicate records in the result set and display a distinct list of values.

The SQL DISTINCT Keyword Syntax

The general syntax for a simple SQL Distinct is below. It returns only DISTINCT (different) values by eliminate duplicates in the result set.
SELECT DISTINCT column-names
              FROM table-name;


DISTINCT Keyword Practical Examples

SQL DISTINCT On One Column

Below DISTINCT Statement will return single Column [FirstName] from [Employees] table.
SELECT DISTINCT [FirstName]
    FROM [Employees];

SQL DISTINCT Multiple Columns

Below Distinct Statement will return multiple Columns [FirstName], [LastName] from [Employees] table.
SELECT DISTINCT [FirstName], [LastName],
       FROM [Employees];

SQL DISTINCT Statement on All Columns

If used (*) instead of column name in SQL Statement it will return all DISTINCT rows from [Employees] table.
SELECT DISTINCT (*)
       FROM [Employees];
Caution: It’s not ideal select all columns all the time if only need certain columns as that increase burden on SQL engine. We will discuss its effects on another day!

SQL DISTINCT and WHERE Clause

The following will list the DISTINCT employees and limit the result set to the Where Clause
SELECT DISTINCT [FirstName]
    FROM [Employees];
        WHERE FirstName= ='David';


SQL DISTINCT Count

When SQL COUNT () used with DISTINCT it eliminate the duplicate records of the same data and only count distinct records once.
SELECT COUNT (DISTINCT [FirstName])
    FROM [Employees];


SQL DISTINCT and NULL Values

SQL DISTINCT eliminate the duplicate records of the Null data and only count single Null record.





Friday, 6 September 2019

Introduction to SQL - SELECT Statement


SQL SELECT Statement

SQL SELECT statement pulls data from a table in the SQL database and display it in a table like structure called a result-set. You can select single column or multiple columns at once. Just list the column name or column names separated by commas from the table:

The SQL Statement Syntax

The general syntax for a simple SQL statement is

SELECT column-names
              FROM table-name;


Practical Examples


SQL SELECT Statement - Single Column

Below SQL Statement will return single Column [FirstName] from [Employees] table.

SELECT [FirstName]
            FROM [Employees];

SQL SELECT Statement – Multiple Columns
Below SQL Statement will return multiple Columns [FirstName],[LastName] from [Employees] table.

SELECT [FirstName], [LastName],
       FROM [Employees];

SQL SELECT Statement – All Columns

If used (*) instead of column name in SQL Statement it will return all Columns from [Employees] table. 

SELECT *
       FROM [Employees];

Caution: It’s not ideal to use Select all columns all the time if only need certain columns as that increase burden on SQL engine. We will discuss its effects on another day!


This concludes our study of the SQL Select Statement. In the next article, we will take a closer look at  DISTINCT and WHERE Keywords and how these used in SQL.

Below is a list of my favorite SQL books which I would recommend for you if you looking to enhance your knowledge on the topic.




Below is my favourite Forums for SQL   






Thursday, 29 August 2019

SQL Syntax - How to Query SQL Database


SELECT
SELECT [FirstName] FROM [Employees];

DISTINCT
SELECT DISTINCT [FirstName]
FROM [Employees];
 
WHERE
SELECT [FirstName]
FROM [Employees]
WHERE [FirstName] ='Ken';

AND 
SELECT [FirstName]
FROM [Employees]
WHERE [FirstName] ='David' AND [FirstName] ='Ken';

OR
SELECT [FirstName]
FROM [Employees]
WHERE [FirstName] ='David' OR [FirstName] ='Ken';

IN  
SELECT [FirstName]
FROM [Employees]
WHERE [FirstName] IN ('David','Ken','Smith');

BETWEEN
SELECT [FirstName]
FROM [Employees]
WHERE [EmployeeID] BETWEEN 1 AND 5;

LIKE
SELECT [FirstName]
FROM [Employees]
WHERE [FirstName] LIKE 'David' ;

ORDER BY
SELECT [FirstName]
FROM [Employees]
ORDER BY [EmployeeID] ASC;

COUNT
SELECT COUNT ([FirstName])
FROM [Employees];

GROUP BY 
SELECT [FirstName], Max (Salary) 
FROM [Employees]
Group by [FirstName];

HAVING
SELECT [FirstName], Max (Salary)
FROM [Employees]
Group by [FirstName]
Having [FirstName]='David';

CREATE TABLE
CREATE TABLE [Employee]
(EmployeeID Int,
 EmployeeName Varchar(50));

DROP TABLE
DROP TABLE [Employee];
Truncate Table
TRUNCATE TABLE [Employees];

Insert Into
INSERT INTO  [Employees]
VALUES ('David','Leach');

Update
UPDATE [Employees]
SET FirstName='David';

Delete From
DELETE FROM [Employees]

Where FirstName='David';


This concludes our study of the basic syntax of SQL. In the next article, we will take a closer look at each if these Keywords and how these used in SQL.

Below is a list of my favorite SQL books which I would recommend for you if you looking to enhance your knowledge on the topic.

SQL in 10 Minutes, Sams Teach YourselfSQL: The Ultimate Beginner's Guide to Learn SQL Programming Step by Step 
Learning SQL
SQL Cookbook (Cookbooks (O'Reilly))
SQL Queries for Mere Mortals: A Hands-On Guide to Data Manipulation in SQL
T-SQL Fundamentals
Data Analysis Using SQL and Excel, 2nd Edition 


Below is my favourite Forums for SQL   
SQL Authority with Pinal Dave 
SQL Server Central Articles