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



Sunday, 30 June 2019

SQL Syntax


For each instruction here; let's call it each command as SQL like commands. First we will list and explain the syntax of the each command and then use an example to let  you digest how the command is used. Once you have read all the materials on this topic, you will have a general understanding of the syntax of SQL. In addition, you will be able to use SQL correctly to get information from the database. The author's own experience is that although a thorough understanding of SQL is not possible overnight, it is not difficult to have a basic understanding of SQL. I hope that after reading this here, you will have the same idea. Add this website to "My Favorites" now so that you can come back later at any time which you do by pressing 'Ctrl+D' button on your keyboard.


If you are teaching a course about SQL or a database, you are welcome to connect to this website for assistance. In this article we will look at examples and discuss the core keywords and basic syntax of the SQL language. All SQL statement begin with one of the following keywords which generally command or instruct SQL that what a user wants to do:

Keywords
Description
Select
The SELECT statement is used to select data from a database.

Distinct
The DISTINCT keyword is used to select distinct data from a database.

Where

The WHERE clause is used to select distinct records from a database

And/or

The AND Operators is used return result where both conditions satisfied.

OR
The OR Operators is used return result where one condition met. 

IN

The IN keyword is used to specify multiple values in the where clause.

Between

The BETWEEN Operator is used to select values in a range of data.

Like

The LIKE operator is used in a WHERE clause to search for a specific pattern of a data in a column.

Order By

The ORDER BY keyword is used to keyword is used to sort the result set in ascending or descending order.

Count

The COUNT function is used to count the number of rows that matches specified criteria.

Group By

The GROUP BY statement group same values into a group.

Having

The HAVING clause used instead of Where clause where aggregate functions used.

Create Table

The CREATE TABLE statement is used to create a new table in a database.

Drop Table

The DROP TABLE statement is used to drop an existing table in a database.

Truncate Table

The TRUNCATE TABLE statement is used to empty the table by truncating.

Insert Into

The INSERT INTO statement is used to insert new records in a table.
Update

The UPDATE statement is used to update data in one or more records in the table.

Delete From

The DELETE FROM statement is used to update data in one or more records in the table.































































This concludes our first part of the basic concepts of the main keywords used in SQL databases. In the next article, we will take a closer look at How to write basic SQL query using these keywords.                         
Below is a list of my favorite SQL books which I would recommend if you looking to enhance your knowledge on the SQL databases. 
                                                                                            


Below is my favourite Forums for SQL