Coder Social home page Coder Social logo

dbms_ex_05's Introduction

EXP No.5

Date:16/04/2024

AIM:

To Study & Implementation of Sub queries Views .

THEORY:

SUBQUERIES:

The query within another is known as a subquery. A statement containing a subquery is called a parent statement. The rows returned by subquery are used by the parent statement or in other words A subquery is a SELECT statement that is embedded in a clause of another SELECT statement You can place the subquery in a number of SQL clauses:

  1. WHERE clause
  2. HAVING clause
  3. FROM clause
  4. OPERATORS( IN.ANY,ALL,<,>,>=,<= etc..)

Types:

1. Sub queries that return several values

Sub queries can also return more than one value. Such results should be made use along with the operators in and any.

2. Multiple queries

Here more than one subquery is used. These multiple sub queries are combined by means of ‘and’ & ‘or’ keywords.

3. Correlated subquery

A subquery is evaluated once for the entire parent statement whereas a correlated Sub query is evaluated once per row processed by the parent statement.

VIEW:

In SQL, a view is a virtual table based on the result-set of an SQL statement. A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database. You can add SQL functions, WHERE, and JOIN statements to a view and present the data as if the data were coming from one single table. A view is a virtual table, which consists of a set of columns from one or more tables. It is similar to a table but it does not store in the database. View is a query stored as an object.

Syntax:

CREATE VIEW AS SELECT FROM relation_name WHERE (Condition)

DROPPING A VIEW:

A view can be deleted with the DROP VIEW command.

Syntax:

DROP VIEW ;

MODULE:

QUESTION 1:

image

QUERY:

SELECT name,city
FROM customer
WHERE city in (SELECT city FROM customer WHERE id in (3,7))

OUTPUT:

image

QUESTION 2:

image

QUERY:

SELECT medication_id AS medic,medication_name,MIN(dosage) AS dosage
FROM Medications;

OUTPUT:

image

QUESTION 3:

image

QUERY:

WITH MinGrades AS (
    SELECT subject, MIN(grade) AS min_grade
    FROM Grades
    GROUP BY subject
)
SELECT g.student_id, g.student_name, g.subject, g.grade
FROM Grades g
JOIN MinGrades mg ON g.subject = mg.subject AND g.grade = mg.min_grade;

OUTPUT:

image

QUESTION 4:

image

QUERY:

SELECT *
FROM customer
WHERE customer_id=(SELECT salesman_id -2001
                   FROM salesman
                   WHERE name= 'Mc Lyon');

OUTPUT:

image

QUESTION 5:

image

QUERY:

SELECT id, name, city, email, phone
FROM customer
WHERE city <> (
    SELECT city
    FROM customer
    WHERE id = (SELECT MAX(id) FROM customer)
);

OUTPUT:

image

QUESTION 6:

image

QUERY:

SELECT ord_no,purch_amt,ord_date,customer_id,salesman_id
FROM orders
WHERE 
    purch_amt > (
        SELECT AVG(purch_amt)
        FROM orders
        WHERE ord_date = '2012-10-10'
    );

OUTPUT:

image

QUESTION 7:

image

QUERY:

SELECT id, name, age, city, income
FROM Employee
WHERE age < (
    SELECT AVG(age)
    FROM Employee
    WHERE income > 250000
);

OUTPUT:

image

QUESTION 8:

image

QUERY:

SELECT id, name, age, city, income
FROM Employee
WHERE age < (
    SELECT AVG(age)
    FROM Employee
    WHERE income > 250000
);

OUTPUT:

image

QUESTION 9:

image

QUERY:

SELECT name
FROM customer c1
WHERE NOT EXISTS (
    SELECT 1
    FROM customer c2
    WHERE c1.phone = c2.phone AND c1.id <> c2.id
);

OUTPUT:

image

QUESTION 10:

image

QUERY:

SELECT ord_no, purch_amt, ord_date, customer_id, salesman_id
FROM orders
WHERE salesman_id IN (
    SELECT salesman_id
    FROM orders
    WHERE customer_id = 3007
);

OUTPUT:

image

RESULT:

Thus,we studied & Implemented the Sub queries Views .

dbms_ex_05's People

Contributors

mena-rossini avatar vanisha0609 avatar

Forkers

deeksha78

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.