Quantcast
Channel: SQL PraRup – SQL Server Mentalist
Viewing all articles
Browse latest Browse all 121

BISQL – Laymen to SQL Developer # 28 – SQL #2 – Multi Table Queries, Data Manipulation Language

$
0
0

Hi Folks,

This post is part of Series Database Management Systems

Currently running topic for this series is listed as below :

Series of Database Management Systems

>>Chapter 1 : DBMS [Database Management Systems]

>>Chapter 2 : Database Core Concepts and  Applications

>>Chapter 3 : Record Storage and Primary File Organization

>>Chapter 4 : Index Structures of Files

>>Chapter 5 : Entity-Relationship Model

>>Chapter 6 : Relational Algebra

>>Chapter 7 : SQL<You are Here>

Continuing from my previous post on this series.

We are going to Cover the Following Points in this article

  • Multi Table Queries
  • Data Manipulation Language

Multi Table Queries

So far the queries that we have discussed were containing only one table in the clause. There are many occasions in the database applications where we need to retrieve data from more than one table. This section addresses these kinds of queries.

SIMPLE EQUI-JOINS:

When two tables are joined together we must follow these guidelines:

Ø Table names in the FROM clause are separated by commas.

Ø Use appropriate joining condition. This means that the foreign key of table 1 will be made equal to the primary key of table 2. This column acts as the joining attribute. For example, dno of employee table and dno of department will be involved in the joining condition of WHERE clause.

Ø EXAMPLE-1: This example demonstrates the equijoin and the purpose is to display the employee names and the department names for which they work.

Ø OUTPUT:

SELECT NAME, DNAME

FROM Employee, Department

WHERE employe.Dno = department.Dno;

NAME DNAME

Prasad Accounts

Reena Accounts

Deepak Admin

Venkat Accounts

Pooja Research

SELF JOIN and TABLE ALIASES:

The self-join is one where you involve the same table in the join. This is illustrated in the following example. This technique is used fully to solve many queries.

Ø To find the employee who earns more than venkat

SELECT e1.name, e1.salary

FROM Employee e1, Employee e2

WHERE (e1.salary > e2.salary) AND (e2.name = ‘venkat’)

Ø OUT PUT:

NAME SALARY Prasad 32000

OUTER JOINS:

Outer joins are used to display rows that do not meet the join condition. For left outer join use a plus sign (+) to left condition and for right outer join use the plus sign to the right condition. The syntax for left and right outer joins is given below:

Left outer join

SELECT table1.col, table2.col

FROM table1 t1, table2 t2

WHERE t1.col (+) = t2.col;

Notice that the plus sign cannot be placed on both sides of the condition.

Ø EXAMPLE 1: This example demonstrates the right outer join by retaining the right side table (department) tuples and giving null values for the tuples that do not match the left side table (employee).

SELECT Name, Dname

FROM Employee E, Department D WHERE E.Name(+) =D.Dname;

Ø OUTPUT::

NAME DNAME Accounts Admin

Nested Queries or Sub queries

A where clause generally contains a condition; but it can also contain an SQL query. The query within a WHERE clause is called the inner query or sub query, and the query that encloses the inner one is called as outer query or main query. It is also possible to place the inner query with in a FROM or HAVING clause. Using nested queries it is possible to build powerful SQL programs.

Execution of nested queries:

The general syntax of a nested query is given below.

image

The operator mentioned in the outer query can be any one of >, =, or IN. Normally the outer query uses the result of the inner query to display the values of columns mentioned in the outer query.

Single-Row Nested Queries

The simplest single-row nested query is by using = sign.

Ø EXAMPLE: Assume that we wish to display the names and the employees working for accounts department.

SELECT Name

FROM Employee

WHERE Dno =

Ø OUTPUT:

NAME

Prasad

Reena

Venkat

(SELECT DNo

FROM Department

WHERE Dname = ‘accounts’);

GROUP BY clause in SUB QUERIES:

Display all the employees drawing more than or equal to the average salary of department number 3.

SELECT Name, Salary

FROM Employee

WHERE Salary >=

(SELECT  AVG(salary)

FROMEmployee

Group B’ DNO Having dnO = 3);

Ø OUT PUT NAME

SALARY

Prasad

32000

Venkat

30000

Multiple Row Nested Queries

The operators IN, ANY, and ALL are used in the multiple row sub queries. The sub query in this case returns more than one row.

OPERATORS

DESCRIPTION

IN

Equal to any member in the list

ANY

Compare value to each value returned by the sub query

ALL

Compare value to all the values returned by the sub query.

Ø EXAMPLE: Display the name of the highest paid employee, SELECT Name, salary

FROM Employee

WHERE Salary IN

(SELECT MAX (Salary) FROMEmployee)

Remember that the multiple row sub queries expect one or more results. In this example the inner query gives a single value and the next example shows a set of values. The following table gives an idea of how to use ANY and ALL.

OPERATOR

MEANING

EXAMPLE

<ANY

Less than the maximum

e<ANY (5.3.8) e is less than any single item in the life (5, 3, 8). Even 7 qualifies, because

7<8

>ANY

More than the minimum

e>ANY (5, 3, 8): e is less than any single item in the list (5, 3, 8). Even 4 qualities, because

4>3.

<ANY

Same as IN

e = any (5, 3, 8). ALL value in the list quality,

<ALL

Less than the maximum

e <ALL (5, 3, 8); anything below 3 qualifies.

>ALL

More than the maximum

e > ALL (5,3,8) : anything greater than 8 qualifies

!=ALL

Not equal to any thing

E! = (5, 3, 8): anything other than 5, 3 and 8 qualifies.

Ø EXAMPLE 1:

SELECT Name, salary

FROM Employee

WHERE Salary< ANY

(SELECT Salary

FROMEmployee

WHERE DNo =3);

Ø OUTPUT:

NAME SALARY

Reena 8000

Deepak 22000

Venkat 30000

Pooja 18000

In this example, all the rows qualify except the row with salary 8000, because all employees draw more than the minimum salary in the result of the sub query. If your condition is >= then you get all the rows.

Data Manipulation Language

A data manipulating language [DML] consists of SQL statements that are used to insert, delete and update the records in a table.

INSERT STATEMENT:

To add a new row into the table you can follow the syntax: INSERT INTO table [(column-1I, column-2…I)]

Values (value- I, value-2…..I);

Using this syntax you can insert only one row at a time. To insert more than one row, you can execute the insert statement repeatedly. The simplest example for INSERT statement is shown below.

Ø EMPLOYEE

INSERT INTO Employee

VALUES (1111, ‘deepak’, ’5-jan-82′, 0000, 4444,); To enter more records we can use / (slash symbol)

‘/’ is used to execute the commands stored in the buffer

Insert into emp(empno,eaddr,basic)

values (&empno,’&eaddr’,&ba);

DELETE OMMAND

It is a DML statement to delete record(s) Syntax: DELETE FROM table

[WHERE cond]; / / If the WHERE condition is not present in the query, all the rows in the table are deleted.

Example: delete from emp where name = ‘Yadav’;

UPDATE COMMAND

It is used to change existing values in a table. Syntax: UPDATE table

SET col I = val I, col2 = val2……

[WHERE cond];

update emp set deptno = 100; / / If the WHERE condition is not present in the query, all the rows in the table are upaed.

Update emp set ename= ‘Sourav’ where empno = 100; Transaction Control Language It is used to control transaction Eg: Commit

Save changes permanently in the database.

Roll back: Discard/Cncel the changes upto the previous commit point. Save point:

Is used to commit / Rollback particular point.

Ex: Commit………………

Insert…………………

Update………………..

Savepoint aa

Delete

#

#

#

Rollback to aa.

Commit to creating And Altering Database objects (DDL):

The basic notion of this section is to introduce the ways to create and manipulate the following database objects.

Ø Table: A tabular structure that stores data.

Ø

View:

A tabular structure similar to a table but it is a

   

collection of one or more tables.

Ø

Sequence

Automatically generates a sequence of numbers

Ø

Index:

Provides an efficient access structure.

 

Hope you will like Series of Database Management Systems series !

If you have not yet subscribe this Blog , Please subscribe it from “follow me” tab !

So that you will be updated @ real time and all updated knowledge in your mail daily for free without any RSS subscription OR news reading !!

Happy Learning and Sharing !!

For More information related to BI World visit our all Mentalist networks Blog

SQL Server Mentalist … SQL Learning Blog

Business Intelligence Mentalist … Business Intelligence World

Microsoft Mentalist … MVC,ASP.NET, WCF & LinQ

MSBI Mentalist … MS BI and SQL Server

NMUG Bloggers …Navi Mumbai User Group Blog

Architectural Shack … Architectural implementation and design patterns

DBA Mentalist …Advance SQL Server Blog

MVC Mentalist … MVC Learning Blog

Link Mentalist … Daily Best link @ your email

Infographics Mentalist … Image worth explaining thousand Words

Hadoop Mentalist … Blog on Big Data

BI Tools Analysis … BI Tools

Connect With me on

| Facebook |Twitter | LinkedIn| Google+ | Word Press | RSS | About Me |


Filed under: Link, Microsoft SQL Server, MSBI, Optimization, Query, SQL Mentalist, SQL PraRup, SQL Query, SQL Server, Technology,, Vishal Pawar Tagged: DBA, Layman to SQL Server, Learn SQL Server, SQL Server, SQL Server 2008 R2, SQL Server Basic, SQL server Understanding, Tricks of SQL Server, Vishal Pawar

Viewing all articles
Browse latest Browse all 121

Trending Articles