Use this space to put some text. Update this text in HTML

468x60 banner ad

Advertise with Us

Powered by Blogger.

Tuesday 23 February 2016

sql-select-query

SQL Select Query

SQL SELECT statement is used to retrieve the data from a table in the database. 
sql select query retrieve data from particular column or all columns in the table.  
the query result is called result-sets.

Syntax of SQL SELECT Statement:

SELECT Column1, Column2, Column3, ColumnN
FROM Table_Name

Here, column1, column2...are the fields of a table whose values you want to fetch. 
  • Table_Name through which data is retrieved. 
  • columns which you want to retrieved the data from one or more column.

  Example:

Consider the EMPLOYEE table having the following records:








If you want to fetch all the fields of EMPLOYEE table, then use the following query:

SELECT * FROM Employee












If you want to fetch Employee_id, First_name, Salary fields of EMPLOYEE table, then use the following query:

SELECT Employee_id, First_name, Salary 
FROM Employee



 

WHERE Clause in SELECT Statement:

The WHERE Clause is used to retrieve the specific information on the basis of condition. the conditional operation give you the exact result whatever you wants.   

For example, when you want to see the information about Employee whose salary not greater than 700000. Retrieving information about all the Employee would increase the processing time for the query.

Example

SELECT First_name, salary
FROM employee
WHERE Salary > 700000;

Result



 
 

sql-is-not-null-and-is-not-empty-string

SQL Is Not Null or Not Empty 

 How to Check for Is Not Null or Not Empty Column in SQL Server SQL

A Not Null means the value in the column is not null (means it has some value in the column). You have to use a clause in SQL IS Not Null.

On the other hand, not empty string is an actual value that can be compared to in a database. You simply use two ticks together.

Create Table First in SQL Server


CREATE TABLE [dbo].[Employee](

      [Employee_id] [int] IDENTITY(1,1) NOT NULL,

      [First_name] [nvarchar](300) NULL,

      [Last_name] [nvarchar](300) NULL,

      [Salary] [decimal](20, 2) NULL,

      [Joining_date] [datetime] NULL,

      [Department] [nvarchar](200) NULL

) ON [PRIMARY]
 Insert Record 
 
INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('John', 'Abraham', 1000000.00, 1905-06-07, 'Banking')

INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('Michael', 'Clarke', 800000.00, 1894-06-28, 'Insurance')

INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('Michael', 'Clarke', 800000.00, 1894-06-28, 'Insurance')

INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('Roy', 'Thomas', 700000.00, 1894-06-27, 'Banking')

INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('Tom', 'Jose', 600000.00, 1894-06-27, 'Insurance')

INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('Jerry', 'Pinto', 650000.00, 1894-06-27, 'Insurance')

INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('Philip', 'Mathew', 750000.00, 1894-06-28, 'Services')

INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('TestName1', '123', 650000.00, 1894-06-28, 'Services')

INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('TestName2', 'Lname%', 600000.00, 1894-06-27, 'Insurance')
INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('Yamuna', 'Singh',4000.00, NULL, NULL)
INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('Pankaj', 'Roy', 45000.00, NULL, '')

Show Record

select * from [dbo].[Employee]
 


In this above table, I specifically put in some Department that are both not null and not empty strings.

Get the Not NULL values using the IS Not NULL operator.

select * from [dbo].[Employee]
where [Department] is not null

Result






Get the not empty string using the not equal string.


select * from [dbo].[Employee]

where [Department] != ''

Result


  If you want to combine them to search for the SQL is not  null or not empty string together and retrieve all of the empty strings and nulls all at once, you could do something like this.

select * from [dbo].[Employee]
where (([Department] IS NOT NULL) AND ([Department] != ''))

Result



 
 

 

Monday 22 February 2016

sql-create-table

CREATE TABLE (Transact-SQL)


Creating a simple table means:
1. Give the table name what you wants that is uniquely identified in database.

Example:

Tm_Employee, Tm_Department, Tm_Salary etc. 

2. Table contains Rows and Column.
3.Columns contain the name of the column, data type, and any other attributes for the column.
3. Rows contains the values or data of the table column.

Here is Simple Table Example:

Below table name is [dbo].[Employee] and contains the column Employee_id, First_name, Last_name, Salary, Joinning_date and Department

The Employee_id contains the data-type int and it is identity increment column (means Employee_id value automatically increment by one),  
First_name contains the data-type nvarchar(300) 
Last_name contains the data-type nvarchar(300),  
Salary contains the data-type decimal(20,2),
Joining_Date contains data-type datetime,   
Department contains data-type nvarchar(300).  

Create Table First in SQL Server

From the Windows Start Menu --> select “Microsoft SQL Server”--> Click on “SQL Server Management Studio”.

Right Click on your created database (TestDB) -> and Click on New Query -> write the table query as below  ->  and Click Execute.


CREATE TABLE [dbo].[Employee](

      [Employee_id] [int] IDENTITY(1,1) NOT NULL,

      [First_name] [nvarchar](300) NULL,

      [Last_name] [nvarchar](300) NULL,

      [Salary] [decimal](20, 2) NULL,

      [Joining_date] [datetime] NULL,

      [Department] [nvarchar](200) NULL

) ON [PRIMARY]

 Shows Table and Column


 Insert Record 

Write the table query as below -> and select all ->  and Click Execute.
 
INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('John', 'Abraham', 1000000.00, 1905-06-07, 'Banking')

INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('Michael', 'Clarke', 800000.00, 1894-06-28, 'Insurance')

Show Records


select * from [dbo].[Employee]



 


 

Saturday 20 February 2016

What is SQL

SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database. According to ANSI (American National Standards Institute), it is the standard language for relational database management systems. SQL statements are used to perform tasks such as update data on a database, or retrieve data from a database. Some common relational database management systems that use SQL are: Oracle, Sybase, Microsoft SQL Server, Access, Ingres, etc. Although most database systems use SQL, most of them also have their own additional proprietary extensions that are usually only used on their system. However, the standard SQL commands such as "Select", "Insert", "Update", "Delete", "Create", and "Drop" can be used to accomplish almost everything that one needs to do with a database. This tutorial will provide you with the instruction on the basics of each of these commands as well as allow you to put them to practice using the SQL Interpreter.


sql-create-database


 SQL consists of a data definition language, data manipulation language and data control language.
 The scope of SQL Server includes data insert, query, update and delete, schema creation and modification, and data access control.

SQL Server Database contains groups of many objects like table, query, stored procedure, function, trigger etc. we can store schema and data in database. schema means declarative syntax to define fields and data types

Create a Database

From the Windows Start Menu --> select “Microsoft SQL Server”--> Click on “SQL Server Management Studio”.

 Once the Management Studio starts -> Select Server Name( that is your pc name example: abc-PC, xyz-PC) and then select Login and enter password. the database is open.

 

In left side of the database there is Object Explorer. In Object Explorer the menu contains Database, Security, Server Objects, Replication and Management.

 

 

Right click the Databases folder and select new Database. Enter a name in the “Database name” text box. For this example, well use the name “TestDB”.


Finally database shows below


SQL CREATE DATABASE Statement Through sql-query

Create Database Syntax
CREATE DATABASE TestDatabaseName;
 
Create Database Example
 
write sql query on query window and click Execute 
 
CREATE DATABASE Test_DB;