Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
27 views
Unit-3 (SQL Query To Except)
rtyfgh
Uploaded by
fortune gamer
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Unit-3(SQL Query to Except) For Later
Download
Save
Save Unit-3(SQL Query to Except) For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
27 views
Unit-3 (SQL Query To Except)
rtyfgh
Uploaded by
fortune gamer
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Unit-3(SQL Query to Except) For Later
Carousel Previous
Carousel Next
Save
Save Unit-3(SQL Query to Except) For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 10
Search
Fullscreen
‘orm of SQL Query SQL DML commands allows the user to manipulate the data in database. Different DML commands are explained in following sections. Basic Structure The basic structure of an SQL expression consists of three clauses : select, from and where. * The. select clause corresponds to projection operation of the relational algebra. It is used to list the attributes desired in the result of a query. + The from clause corresponds to the cartesian product operation of the relational algebra. It lists the relations to be scanned in the evaluation of the expression. «The where clause corresponds to the selection predicate of the relational algebra. It consists of a predicate involving attributes of the relations that appear in the from clause. A typical SQL query has the form : where pi Where, ‘A; - represents an attribute a 1, - represents relation ~~ p - isapredicate (4-1) Scanned with CamScannerDatabase Management Systems 4-2 SQL forms the cartesian product of the relations named in 4 petforms a relational algebra selection using the where clause pre edicate projects the result onto the attributes of the select clause. 4.1.1 select Clause This command is usec records from a relation. > Different forms of select clause are given below : + Form 1: Use of select clause for displaying selected fields Example : Find the names of all publishers in the book relation, SQL> Select publisher_name from book; Output : Above query displays all publisher_names, Publishers name will get displayed repeatedly, from Book relation. Th well as in the results erefore, some * Form 2: Use of select for displaying distinct values For elimination of duj ~ he from, | and d to display all fields/or set of selected fields for all/s, n Pele SQL allows duplicates in relations 3s of SQL expressions, plicates the keyword distinct it . tbo query is rewritten as, ‘word distinct is used. The above SOL> select distinct publisher_name from book; Output ; Scanned with CamScannerment Systems . onase ManED fs sat SQL. allows us to use the keyword a 7 " are not removed. ‘yword all to specify explicitly that duplicates go> select all publishor name from book; Output: —— PUBLISHER_NAME PHI Technical Nirali Technical SciTech + Form 3: Use of select for displaying all fields The asterisk symbol “*” can be used to denote “all attributes”. A select dlause of the form select * indicates that all attributes of all relations appearing in the from clause are selected. Example : SQL> select * from book; Output : iseN |. TITLE | PUB_YEAR | UNIT_PRICE | AUTHOR_NAME PUBLISHER_NAME | 1001 | Oracle 2004 399 Arora PHI 102 | DBMS 2004 400 Basu Technical 2001 Dos 2003, 250 Sinha Nirali 2002 | ADBMS 2004 450 Basu Technical 2003 Unix 2000 300 Kapoor SciTech SQL> select * from author; Output + ee AUTHOR_NAME COUNTRY Arora Us. Kapoor Canada Basu India Sinha Inia Scanned with CamScannerDatabase Management Systems 4-4 7 SQL> select * from publisher; PUBLISHER_NAME PUB_ADD Output : PHI Delhi Technical Pune MainBazar Nira Mumbai SciTech Chennai * Form 4: Select clause with arithmetic expression The select clause may also contain arithmetic expressions involving the operators +, -, *, and/operating on constants or attributes of tuples, Example SQL> select title, unit_price * 10 from book, Output : TITLE UNIT. PRICE * 10 Oracle 3990 DBS a Dos 2500 ADBMS 4500 Unix 3000 : The above query returns a relation that is the same as the book relation with attributes title as it is and unit_price, will get multiplied by 10. 4.1.2 where Clause The where clause is used to select specific rows satisfying given predicate. Example : “Find the titles of books published in year 2004”, This query can be written in SQL as : SQL> select title from book where pub, year ‘2004’; Scanned with CamScanneri gomont Systems 4-5 pst! Oracle DBms uses the logical connectives and, or and not in the where clause. The vnds of logical connectives can be expressions involving the comparison operators _ and <>. SQL allows us to use the comparison operators to compare id arithmetic expressions, as well as special types, such as date types. ger ings om > Between QL includes a between comparison operator to specify that a value be less than cregul to some value and greater than or equal to some other value. xample : “Find the titles of book having price between 300 to 400”. SQL> select Title from Book where Unit_price between 300 and 400; output : Or SQL> select Title from Book where Unit_price >= 300 and Unit_price <= 400; Output : Silay, we can use the not between comparison operator. Scanned with CamScanner4-6 Sau 4.1.3 from Clause ; The from clause defines a cartesian product of the relations in the clause, Example : “Find the titles of books with author name and country published in year 2004” SQL> select Title, Book.author_name, Country from Book, Author, where Book.author_name = Author.author_name and Pub_year = ‘2004’; . Output : TITLE AUTHOR_NAME COUNTRY ‘Oracle Arora us DBMS Basu India ADBMS Basu India Notice that SQL uses the notation relation_name.attribute_name to avoid ambiguity in cases where an attribute appears in the schema of more than one relation. 4.2 Examples of Basic SQL Queries More examples of SQL queries are given in this section. Consider following three relations : 1) Sailors(sid : integer, sname: string, rating: integer, age: real) 2) Boats(bid : integer, bname: string, color: string) 3) Reservesisid : integer, bid: integer, day: date) Query 1) Find the names of sailors who have reserved boat 103. This query can be written in SQL as follows : SELECT sname FROM Sailors S, Reserves R WHERE S.sid sid AND bid= 103; Query 2) Find the sids of sailors who have reserved a red boat. This query can be written in SQL as follows : SELECT Rsid FROM Boats B, Reserves R WHERE B.bid = R.bid AND B.color = ‘red’ i Query 3) Find the names of sailors who have reserved a red boat. This query can be written in SQL as follows : Scanned with CamScanneri LOT ‘Set Operations ' j tks The SQL operations union, intersect and except operate on relations 4, correspond to the relational-algebra operations U, 9 and -- Consider two tables : i) Depositor (Customer_name, Account_no) ii) Borrower (Customer_name, Loan_no) SQL> select * from Depositor; Output : Customer_name Account_no John 1001 Sita 1002 ‘Vishal 1003 Ram 1004 Pf SQL> select * from Borrower; Output : Customer_name Loan_no John 2001 Tonny 2003 Rohit 2004 Scanned with CamScanneraa Management Systems 4- patabas' WW saL ed union Operations union clause merges the output of two or more cries | , a ola queries into a single set of rows ‘Common records. in both queries Records only in query one Records only in query two Fig. 4.1 Output of union clause Output = Records only in query one + Ri only sing! cutee aihich is common ia beth co sigh ReCoras Only in query two + A single set of Baample : Find all customer having a loan, an account or both at the bank. SOL> select Customer_name from Borrower union select Customer_name from Depositor; Output : Customer_name John Ram Rohit Sita Tonny Vishal The union operation automatically eliminates duplicates. "Ewe want to retain all duplicates, we must write union all in place of union. SQL> select Customer_name from Borrower ‘union all Select Customer_name from Depositor; Scanned with CamScanner-12 Database Management Systems 4 CUSTOMER_NAME John SQ. Output : Tonny Rohit Vishal John Sita Vishal Ram The restrictions on using a union are as follows : * Number of columns in all the queries should be same. * The datatype of the column in each query must be same. * Union cannot be used in subqueries. © You cannot use aggregate functions in union. 4.6.2 Intersect Operation The intersect clause outputs only rows produced by both the queries intersected ie. the intersect operation returns common records from the output of both queries. Fig. 4.2 Output of intersect clause Output = A single set of records which are common in both queries. Example : Find all customers who have an account and loan at the bank. SQL> select Customer_name from Depositor intersect select Customer_name from Borrower; Scanned with CamScannerpatabase Management Systoms 4-43 Output : The intersect operation automatically eliminates duplicates. If we want to retain all duplicates we must use intersect all in place of intersect. SOL > select Customer_name from Depositor intersect all select Customer_name from Borrower ; 4.6.3 Except Operation The Except also called as Minus outputs rows that are in first table but not in second table. Output = Records only in query one. Fig. 4.3 Output of the except (Minus) clause Example : Find all customers who have an account but no loan at the bank. SQL> select Customer_name from Depositor minus select Customer_name from Borrower; Customer_name Scanned with CamScanner
You might also like
Chapter 3 - SQL Notes
PDF
No ratings yet
Chapter 3 - SQL Notes
25 pages
(SQL2) نظري 2 PDF
PDF
No ratings yet
(SQL2) نظري 2 PDF
32 pages
Theory
PDF
No ratings yet
Theory
145 pages
Lecture 4 Unit 2
PDF
No ratings yet
Lecture 4 Unit 2
7 pages
COMP163: Database Management Systems September 18, 2008
PDF
No ratings yet
COMP163: Database Management Systems September 18, 2008
52 pages
UNIT-3
PDF
No ratings yet
UNIT-3
64 pages
DBMS UNIT-3 Part-1 SQL
PDF
No ratings yet
DBMS UNIT-3 Part-1 SQL
88 pages
Chapter 6 - Query Languages
PDF
No ratings yet
Chapter 6 - Query Languages
38 pages
Chapter 3 - SQL
PDF
No ratings yet
Chapter 3 - SQL
76 pages
Experiment No (1) - 4 To 5 (DBMS)
PDF
No ratings yet
Experiment No (1) - 4 To 5 (DBMS)
7 pages
Seminar 3 - SQL
PDF
No ratings yet
Seminar 3 - SQL
30 pages
Lecture 04 Week 4
PDF
No ratings yet
Lecture 04 Week 4
46 pages
SQL Interview Questions
PDF
No ratings yet
SQL Interview Questions
11 pages
Module III DBMS
PDF
No ratings yet
Module III DBMS
13 pages
Advance Database Systems - Lec 6 SQL (1)
PDF
No ratings yet
Advance Database Systems - Lec 6 SQL (1)
68 pages
Basic SQL: Section 4.1-4.7
PDF
No ratings yet
Basic SQL: Section 4.1-4.7
30 pages
DBS Unit II
PDF
No ratings yet
DBS Unit II
105 pages
Introduction To Standard Query Language: Erik Zeitler Udbl Erik - Zeitler@it - Uu.se
PDF
No ratings yet
Introduction To Standard Query Language: Erik Zeitler Udbl Erik - Zeitler@it - Uu.se
42 pages
SQL Lecture
PDF
No ratings yet
SQL Lecture
28 pages
Lecture 2 - SQL - template
PDF
No ratings yet
Lecture 2 - SQL - template
26 pages
5.SQL
PDF
No ratings yet
5.SQL
76 pages
Advanced SQL
PDF
No ratings yet
Advanced SQL
46 pages
Sql extra
PDF
No ratings yet
Sql extra
11 pages
9.1 - Chapter 6 - Simple Queries in SQL
PDF
No ratings yet
9.1 - Chapter 6 - Simple Queries in SQL
54 pages
Chapter 6 - Simple Queries in SQL
PDF
No ratings yet
Chapter 6 - Simple Queries in SQL
164 pages
Form of Basic SQL Query in DBMS
PDF
No ratings yet
Form of Basic SQL Query in DBMS
15 pages
Basic Commands of SQL
PDF
No ratings yet
Basic Commands of SQL
63 pages
Data Definition Commands-1
PDF
No ratings yet
Data Definition Commands-1
54 pages
Joins Views Indexes: What Is The Difference Between SQL and PL/SQL?
PDF
No ratings yet
Joins Views Indexes: What Is The Difference Between SQL and PL/SQL?
13 pages
SQL
PDF
No ratings yet
SQL
42 pages
DBMS Unit 3
PDF
No ratings yet
DBMS Unit 3
98 pages
SQL-99 (1)
PDF
No ratings yet
SQL-99 (1)
73 pages
Structured Query Language (SQL)
PDF
No ratings yet
Structured Query Language (SQL)
61 pages
Unit-2 Ppt SQL and Pl SQL
PDF
No ratings yet
Unit-2 Ppt SQL and Pl SQL
26 pages
DBMS UNIT-3 Notes
PDF
100% (3)
DBMS UNIT-3 Notes
45 pages
SQL Clause What It Does Required
PDF
No ratings yet
SQL Clause What It Does Required
3 pages
Lab Manual 02 (Basics)
PDF
No ratings yet
Lab Manual 02 (Basics)
8 pages
DBMS M3 Ktunotes.in 1
PDF
No ratings yet
DBMS M3 Ktunotes.in 1
210 pages
4 SQLQueries
PDF
No ratings yet
4 SQLQueries
52 pages
10 - Chapter 6 - Simple Queries in SQL
PDF
No ratings yet
10 - Chapter 6 - Simple Queries in SQL
36 pages
SQL Query Tutorial
PDF
No ratings yet
SQL Query Tutorial
12 pages
Advance SQL
PDF
No ratings yet
Advance SQL
103 pages
Basic Queries in SQL:: SELECT FROM WHERE
PDF
No ratings yet
Basic Queries in SQL:: SELECT FROM WHERE
13 pages
Structured Query Language (SQL)
PDF
No ratings yet
Structured Query Language (SQL)
25 pages
Week2 & 3(part 1)
PDF
No ratings yet
Week2 & 3(part 1)
19 pages
11.SQL Queries-set Operators
PDF
No ratings yet
11.SQL Queries-set Operators
11 pages
ch4
PDF
No ratings yet
ch4
35 pages
RDBMS
PDF
No ratings yet
RDBMS
49 pages
SQL Notes
PDF
No ratings yet
SQL Notes
20 pages
Chapter-3
PDF
No ratings yet
Chapter-3
88 pages
Lesson 4
PDF
No ratings yet
Lesson 4
76 pages
Week 5 - SQL - Select
PDF
No ratings yet
Week 5 - SQL - Select
22 pages
SQL_PPT1
PDF
No ratings yet
SQL_PPT1
125 pages
Module4 Partial
PDF
No ratings yet
Module4 Partial
71 pages
Chapter 8 (Module 2)
PDF
No ratings yet
Chapter 8 (Module 2)
74 pages
SQL Select: Database Systems Lecture 7
PDF
No ratings yet
SQL Select: Database Systems Lecture 7
50 pages
Screenshot 2024-12-20 at 10.04.15 PM
PDF
No ratings yet
Screenshot 2024-12-20 at 10.04.15 PM
93 pages
pt3-1
PDF
No ratings yet
pt3-1
2 pages