Data Base Management System - Basic Structure of SQL Queries
823 views
12 slides
Mar 04, 2024
Slide 1 of 12
1
2
3
4
5
6
7
8
9
10
11
12
About This Presentation
DBMS - SQL Queries with an example
Size: 85.95 KB
Language: en
Added: Mar 04, 2024
Slides: 12 pages
Slide Content
Basic Structure of SQL Queries Mrs.R.SABITHA ., M.Sc.,M.Phil ., Assistant Professor, Department of Computer Science(SF) V.V.Vanniaperumal College for Women, Virudhunagar .
OUTLINE Overview of the SQL Query Language Data Definition Basic Query Structure Additional Basic Operations Set Operations Null Values Aggregate Functions Nested Sub queries
Overview of the SQL Query Language SQL is a language to operate databases; it includes Database Creation, Database Deletion, Fetching Data Rows, Modifying & Deleting Data rows, etc. SQL stands for Structured Query Language which is a computer language for storing, manipulating and retrieving data stored in a relational database .
SQL Basic Commands DDL (Data Definition Languages) 1. Create - Creates a new table, a view of a table, or other object in the database. 2. Alter - Modifies an existing database object, such as a table. 3. Drop - Deletes an entire table, a view of a table or other objects in the database .
SQL Basic Commands DML - Data Manipulation Language 1. SELECT - Retrieves certain records from one or more tables. 2 . Update – Modify records 3 .Delete – Delete Records
Basic Query Structure A typical SQL query has the form: select A 1 , A 2 , ..., A n from r 1 , r 2 , ..., r m where P A i represents an attribute R i represents a relation P is a predicate. The result of an SQL query is a relation .
Select Clause The select clause lists the attributes desired in the result of a query corresponds to the projection operation of the relational algebra Example: find the names of all instructors: select name from instructor NOTE: SQL names are case insensitive (i.e., you may use upper- or lower-case letters.) E.g., Name ≡ NAME ≡ name
Select Clause SQL allows duplicates in relations as well as in query results. To force the elimination of duplicates, insert the keyword distinct after select. Find the department names of all instructors, and remove duplicates select distinct dept_name from instructor The keyword all specifies that duplicates should not be removed. select all dept_name from instructor
The from Clause The from clause lists the relations involved in the query Corresponds to the Cartesian product operation of the relational algebra. Find the Cartesian product instructor X teaches select X from instructor, teaches generates every possible instructor – teaches pair, with all attributes from both relations. For common attributes (e.g., ID ), the attributes in the resulting table are renamed using the relation name (e.g., instructor.ID )
The where Clause The where clause specifies conditions that the result must satisfy Corresponds to the selection predicate of the relational algebra. To find all instructors in Comp. Sci. dept select name from instructor where dept_name = ‘ Comp. Sci .' To find all instructors in Comp. Sci. dept with salary > 80000 select name from instructor where dept_name = ‘ Comp. Sci.' and salary > 80000
Examples Find the names of all instructors who have taught some course and the course_id select name, course_id from instructor , teaches where instructor.ID = teaches.ID Find the names of all instructors in the Art department who have taught some course and the course_id select name, course_id from instructor , teaches where instructor.ID = teaches.ID and instructor. dept_name = ‘Art’