Computer Science Engineering (CSE)  >  Database Management System (DBMS)  >  Structured Query Language (SQL)

Structured Query Language (SQL) Notes | Study Database Management System (DBMS) - Computer Science Engineering (CSE)

Document Description: Structured Query Language (SQL) for Computer Science Engineering (CSE) 2022 is part of Database Management System (DBMS) preparation. The notes and questions for Structured Query Language (SQL) have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about Structured Query Language (SQL) covers topics like Introduction and Structured Query Language (SQL) Example, for Computer Science Engineering (CSE) 2022 Exam. Find important definitions, questions, notes, meanings, examples, exercises and tests below for Structured Query Language (SQL).

Introduction of Structured Query Language (SQL) in English is available as part of our Database Management System (DBMS) for Computer Science Engineering (CSE) & Structured Query Language (SQL) in Hindi for Database Management System (DBMS) course. Download more important topics related with notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free. Computer Science Engineering (CSE): Structured Query Language (SQL) Notes | Study Database Management System (DBMS) - Computer Science Engineering (CSE)
Table of contents
Introduction
1 Crore+ students have signed up on EduRev. Have you?

Introduction

Structured Query Language is a standard Database language which is used to create, maintain and retrieve the relational database. Following are some interesting facts about SQL.

  • SQL is case insensitive. But it is a recommended practice to use keywords (like SELECT, UPDATE, CREATE, etc) in capital letters and use user defined things (liked table name, column name, etc) in small letters.
  • We can write comments in SQL using “–” (double hyphen) at the beginning of any line.
  • SQL is the programming language for relational databases (explained below) like MySQL, Oracle, Sybase, SQL Server, Postgre, etc. Other non-relational databases (also called NoSQL) databases like MongoDB, DynamoDB, etc do not use SQL
  • Although there is an ISO standard for SQL, most of the implementations slightly vary in syntax. So we may encounter queries that work in SQL Server but do not work in MySQL.
What is Relational Database?

Relational database means the data is stored as well as retrieved in the form of relations (tables). Table 1 shows the relational database with only one relation called STUDENT which stores ROLL_NO, NAME, ADDRESS, PHONE and AGE of students.

STUDENT

TABLE 1 TABLE 1 

These are some important terminologies that are used in terms of relation.

Attribute: Attributes are the properties that define a relation. example: ROLL_NO, NAME etc.

Tuple: Each row in the relation is known as tuple. The above relation contains 4 tuples, one of which is shown as:

Structured Query Language (SQL) Notes | Study Database Management System (DBMS) - Computer Science Engineering (CSE)

Degree: The number of attributes in the relation is known as degree of the relation. The STUDENT relation defined above has degree 5.

Cardinality: The number of tuples in a relation is known as cardinality. The STUDENT relation defined above has cardinality 4.

Column: Column represents the set of values for a particular attribute. The column ROLL_NO is extracted from relation STUDENT.

Structured Query Language (SQL) Notes | Study Database Management System (DBMS) - Computer Science Engineering (CSE)

The queries to deal with relational database can be categories as:

  • Data Definition Language: It is used to define the structure of the database. example: CREATE TABLE, ADD COLUMN, DROP COLUMN and so on.
  • Data Manipulation Language: It is used to manipulate data in the relations. example: INSERT, DELETE, UPDATE and so on.
  • Data Query Language: It is used to extract the data from the relations. example: SELECT

So first we will consider the Data Query Language. A generic query to retrieve from a relational database is:

  1. SELECT [DISTINCT] Attribute_List FROM R1,R2….RM
  2. [WHERE condition]
  3. [GROUP BY (Attributes)[HAVING condition]]
  4. [ORDER BY(Attributes)[DESC]];

Part of the query represented by statement 1 is compulsory if you want to retrieve from a relational database. The statements written inside [] are optional. We will look at the possible query combination on relation shown in Table 1.

Case 1: If we want to retrieve attributes ROLL_NO and NAME of all students, the query will be:

SELECT ROLL_NO, NAME FROM STUDENT;

Structured Query Language (SQL) Notes | Study Database Management System (DBMS) - Computer Science Engineering (CSE)

Case 2: If we want to retrieve ROLL_NO and NAME of the students whose ROLL_NO is greater than 2, the query will be:

SELECT ROLL_NO, NAME FROM STUDENT 

WHERE ROLL_NO>2;

Structured Query Language (SQL) Notes | Study Database Management System (DBMS) - Computer Science Engineering (CSE)

CASE 3: If we want to retrieve all attributes of students, we can write * in place of writing all attributes as:

SELECT * FROM STUDENT 

WHERE ROLL_NO > 2;

Structured Query Language (SQL) Notes | Study Database Management System (DBMS) - Computer Science Engineering (CSE)

CASE 4: If we want to represent the relation in ascending order by AGE, we can use ORDER BY clause as:

SELECT * FROM STUDENT ORDER BY AGE;

Structured Query Language (SQL) Notes | Study Database Management System (DBMS) - Computer Science Engineering (CSE)

Note: ORDER BY AGE is equivalent to ORDER BY AGE ASC. If we want to retrieve the results in descending order of AGE, we can use ORDER BY AGE DESC.

CASE 5: If we want to retrieve distinct values of an attribute or group of attribute, DISTINCT is used as in:

SELECT DISTINCT ADDRESS FROM STUDENT;

Structured Query Language (SQL) Notes | Study Database Management System (DBMS) - Computer Science Engineering (CSE)

If DISTINCT is not used, DELHI will be repeated twice in result set. Before understanding GROUP BY and HAVING, we need to understand aggregations functions in SQL.

AGGRATION FUNCTIONS: Aggregation functions are used to perform mathematical operations on data values of a relation. Some of the common aggregation functions used in SQL are:

  • COUNT: Count function is used to count the number of rows in a relation. example:
    SELECT COUNT (PHONE) FROM STUDENT;

Structured Query Language (SQL) Notes | Study Database Management System (DBMS) - Computer Science Engineering (CSE)

  • SUM: SUM function is used to add the values of an attribute in a relation. example: SELECT SUM (AGE) FROM STUDENT;

Structured Query Language (SQL) Notes | Study Database Management System (DBMS) - Computer Science Engineering (CSE)

In the same way, MIN, MAX and AVG can be used.  As we have seen above, all aggregation functions return only 1 row.

  • AVERAGE: It gives the average values of the tupples. It is also defined as sum divided by count values.
    Syntax: AVG (attributename)
    OR
    Syntax: SUM(attributename)/COUNT(attributename)

The above mentioned syntax also retrieves the average value of tupples.

  • MAXIMUM: It extracts the maximum value among the set of tupples.
    Syntax: MAX(attributename)
  • MINIMUM: It extracts the minimum value amongst the set of all the tupples.
    Syntax: MIN(attributename)
  • GROUP BY: Group by is used to group the tuples of a relation based on an attribute or group of attribute. It is always combined with aggregation function which is computed on group. example: SELECT ADDRESS, SUM(AGE) FROM STUDENT GROUP BY (ADDRESS);

In this query, SUM(AGE) will be computed but not for entire table but for each address. i.e.; sum of AGE for address DELHI(18+18=36) and similarly for other address as well. The output is:

Structured Query Language (SQL) Notes | Study Database Management System (DBMS) - Computer Science Engineering (CSE)

If we try to execute the query given below, it will result in error because although we have computed SUM(AGE) for each address, there are more than 1 ROLL_NO for  each address we have grouped. So it can’t be displayed in result set. We need to use aggregate functions on columns after SELECT statement to make sense of the resulting set whenever we are using GROUP BY.

SELECT ROLL_NO, ADDRESS, SUM(AGE) FROM STUDENT

GROUP BY (ADDRESS); 

Note: An attribute which is not a part of GROUP BY clause can’t be used for selection. Any attribute which is part of GROUP BY CLAUSE can be used for selection but it is not mandatory. But we could use attributes which are not a part of the GROUP BY clause in an aggregrate function.

The document Structured Query Language (SQL) Notes | Study Database Management System (DBMS) - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Database Management System (DBMS).
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)

Related Searches

MCQs

,

Summary

,

pdf

,

past year papers

,

Viva Questions

,

Previous Year Questions with Solutions

,

Exam

,

Important questions

,

ppt

,

practice quizzes

,

Free

,

Structured Query Language (SQL) Notes | Study Database Management System (DBMS) - Computer Science Engineering (CSE)

,

Sample Paper

,

video lectures

,

Semester Notes

,

Structured Query Language (SQL) Notes | Study Database Management System (DBMS) - Computer Science Engineering (CSE)

,

Extra Questions

,

mock tests for examination

,

study material

,

shortcuts and tricks

,

Structured Query Language (SQL) Notes | Study Database Management System (DBMS) - Computer Science Engineering (CSE)

,

Objective type Questions

;