Showing posts with label SQL Fundamental. Show all posts
Showing posts with label SQL Fundamental. Show all posts

Monday, December 12, 2016

Combining statement with conditional operators

Operators “AND”, “OR”
Sometime we need to combine two or more TSQL statement based on some conditions, that we can achieve using Conditional operator, Boolean   “AND operator” and “OR operator”

Boolean Operator: AND (&), OR (||)

Conditional Operator:-  
1.       GREATER THEN EQUAL    (>=) 
                X>Y;
2.       LESS THEN EQUAL ( <=)
Y<X;
3.       EQUAL ( ==)
                X=Z;
4.       GREATER THEN ( >)
                X>Y;
5.       LESS THEN (<)
Y<Z;
6.       NOT EQUAL TO ( <>)
Y<>Z;

The AND operator can be used to join two or more conditions in the WHERE clause. Both sides of the AND condition must be true in order for the condition to be met and for those rows to be displayed.

For example:
SELECT COLUMN1, COUNT (COLUMN2) FROM TABLENAME WHERE "CONDITION1" AND 
"CONDITION2";

The OR operator can be used to join two or more conditions in the WHERE clause. However, either side of the OR operator can be true and the condition will be encountered - hence, the rows will be displayed. With the OR operator, either side can be true or both sides can be true.

For example:
SELECT EMPLOYEEID, FIRSTNAME, LASTNAME, TITLE, SALARY FROM EMPLOYEE WHERE BASIC_PAY >= 15000.00 AND TITLE = 'AVP';

This statement will select the EMPLOYEEID, FIRSTNAME, LASTNAME, TITLE, SALARY from the employee table where the BASIC pay is greater than or equal to 15000 and the title is equal to AVP. Both of these conditions must be true in order for the rows to be returned in the query. If either is false, then it will not be displayed.
Although they are not required, you can use parenthesis around your conditional expressions to make it easier to read:

For example:
SELECT EMPLOYEEID, FIRSTNAME, LASTNAME, TITLE, SALARY FROM EMPLOYEE WHERE (BASIC_PAY >= 15000.00) AND (TITLE = 'AVP');

Another statement get the result set basic_pay>15000 and title can be AVP or VP.


For example:
SELECT EMPLOYEEID, FIRSTNAME, LASTNAME, TITLE, SALARY FROM EMPLOYEE WHERE (BASIC_PAY >= 15000.00) AND (TITLE = 'AVP' OR TITLE=’VP’);


Sunday, December 11, 2016

Aggregate Functions

Aggregate Functions

MIN
Returns the smallest value in a given column
MAX
Returns the largest value in a given column
SUM
Returns the sum of the numeric values in a given column
AVG
Returns the average value of a given column
COUNT
Returns the total number of values in a given column
COUNT(*)
Returns the number of rows in a table

In database management system an aggregate function is a function where the values of multiple rows are grouped together as input and on certain criteria to form a single value. Aggregate functions are used to compute against a numeric column data from SELECT statement. This mostly summarize the results of a particular column of selected data. 

For example:

SELECT AVG (MARKS) FROM STUDENTS;

This statement will return a single result which contains the average value of everything returned in the Marks column from the Students table.
Another example:

SELECT AVG (SALARY) FROM EMPLOYEE WHERE TITLE=’AVP’;

This statement will return the average salary for all employee whose title is equal to AVP.

Example:

SELECT COUNT (*) FROM STUDENTS;

This statement returns the total number of students. However you can also add additional column or distinct values counts.

Example:-


SELECT COUNT (DISTINCT TITLE) FROM EMPLOYEE;

Monday, February 8, 2016

SQL Create Table


SQL CREATE TABLE
The CREATE TABLE statement is used to create a table in a database to stored data.
Tables are organized into rows and columns; and each table must have a name.
SQL CREATE TABLE Syntax
CREATE TABLE tablename
(
columnname1 datatype(size) [null | Not Null],
columnname2 datatype(size) [null | Not Null],
columnname3 datatype(size) [null | Not Null],
....
);
Parameters
tablename
The name of the table that you wish to create.
columnname1, columnname2
The columns that you wish to create in the table. Each column must have a datatype. The column should either be defined as NULL or NOT NULL and if this value is left blank, the database assumes NULL as the default.
CREATE TABLE employees
( employeeid INT NOT NULL,
  lastname VARCHAR(50) NOT NULL,
  firstname VARCHAR(50), 
  Address varchar(200)
  areaPin bigint
  );
  1.     Column name employeeid is numeric column and can’t be null
  2.        Lastname, firstName,Address is varchar column.
  3.      AreaPin is numeric column.



SQL Basics for Beginner

SQL Basics
An instruction to a database to combine data from more than one table.
A SQL join combines records from two or more tables in a relational database. It creates a set that can be saved as a table or used as it is. A JOIN is a means for combining fields from two tables (or more) by using values common to each.
DATABASE
A database is a collection of information that is organized so that it can easily be accessed, managed, and updated. In one view, databases can be classified according to types of content: bibliographic, full-text, numeric, and images.
RELATIONAL DATA
A database structure that is in relationship with other database objects. These links are what we use to do our SQL Joins, so they are important to understand. The name for these links in database terminology is "foreign keys." A foreign key is the way you link one table to another.
This join can be of any type like one-to-one, one-to-many, many-to-many etc.
TABLE
Databases store their data in a system of tables. As we do stored data in excel or access same we do in a tables. Tables is a collection of columns and rows where you store data. Typically, each row represents an additional thing that you care about, and each column represents an attribute that the thing can have. Table can have multiple type of columns like numeric, string, text, image, video etc.

We have employee table which is collection of columns 
(IdNum, Lname,Fname,JobCode,Salary,Phone).

TYPES OF SQL JOINS

SQL join is in a database is combining data from more than one table. There are different kinds of joins, which have different rules for joining.
INNER JOIN
An inner join produces a result set that is limited to the rows where there is a match in both table.  
LEFT OUTER JOIN
A left outer join, or left join, results in a set where all of the rows from the first, or left hand side, table are preserved. The rows from the second, or right hand side table only show up if they have a match with the rows from the first table.  Where there are values from the left table but not from the right, the table will read null, which means that the value has not been set. 
RIGHT OUTER JOIN
A right outer join, or right join, is the same as a left join, except the roles are reversed.  All of the rows from the right hand side table show up in the result, but the rows from the table on the left are only there if they match the table on the right.  Empty spaces are null, just like with the the left join.  
FULL OUTER JOIN
All rows from both tables are returned in a full outer join. Similarly to the left and right joins, we call the empty spaces null.   
CROSS JOIN
The cross join returns a table with a potentially very large number of rows.  The row count of the result is equal to the number of rows in the first table times the number of rows in the second table. Each row is a combination of the rows of the first and second table.  
SELF JOIN
You can join a single table to itself.  We can use same table twice.
You can be perfect from diagram.

Thursday, July 23, 2015

What is SQL?

If you have any question or if you want to add any of your question write a comment, we will include with answer. 

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.