0% found this document useful (0 votes)
21 views

Chap 4 Spatial DBMS - Exercise PostgreSQL

The document provides information on beginning with PostgreSQL including installing PostgreSQL, creating a database, and examples of SQL queries. It then discusses designing a student grade database and provides examples of queries that can be run on such a database.

Uploaded by

Ankur Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Chap 4 Spatial DBMS - Exercise PostgreSQL

The document provides information on beginning with PostgreSQL including installing PostgreSQL, creating a database, and examples of SQL queries. It then discusses designing a student grade database and provides examples of queries that can be run on such a database.

Uploaded by

Ankur Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CHAPTER 4:

BEGINNING WITH POSTGRESQL


T H E W O R L D ' S M O S T A D V A N C E D O P E N S O U R C E
D A T A B A S E

1 HOW TO START POSTGRESQL

The installer will add a PostgreSQL 9.1 menu to your Start menu.

 Navigate to the PostgreSQL 9.1 menu item and run PgAdmin III.

 Double click on the “PostgreSQL Database Server” tree entry. You will be
prompted for the super user password to connect to the “postgres” database.
 Now you are connected to server.

2 HOW TO CREATE DATABASE IN POSTGRESQL

 Navigate to the “Databases” section of the database tree and open “Edit → New
Object → New Database”.Add a new database
named “StudentDB”, with “postgres” as the owner. “postgres” as the template.
 Now StudentDB database has been created in Databases section.
3 SQL QUERY EXAMPLE

Query 1: Gives the all faculty name.

SELECT *
FROM FACULTY;

Query 2: Gives the details of faculties.

SELECT FACULTYNAME, DESIGNATION, SALARY


FROM FACULTY;

Query 3: Gives the details Course offered by university

SELECT COURSENO, NUMCREDIT


FROM COURSE;

Query 4: Show course which are of credit 4 or more.

SELECT COURSENO, COURSENAME, NUMCREDIT


FROM COURSE
WHERE NUMCREDIT >=4;

Query 5: Find out the male student.

SELECT STUDENTNAME, SEX


FROM STUDENT
WHERE SEX = ‘M’;

Query 6: Find out student which are female or greater than 20 year

SELECT STUDENTNAME, CITY, STATE, PINCODE


FROM STUDENT
WHERE SEX= ‘F’
OR
AGE > 20

Query 7: Gives the name of students who are hostler.

SELECT STUDENTNAME, CITY


FROM STUDENT
WHERE HOSTELID < > NULL

Query 8: Find all three credit course and list them alphabetically by course name in
descending order.
SELECT COURSENAME
FROM COURSE
WHERE NUMCREDIT = 3
ORDER BY DEPARTMENT DESC;

Query 9: Gives the Name of Faculty and their salary whose salary becomes 60,000
after the 15 percent increment.

SELECT TEACHERNAME, NEWSAL =1.5*SALARY


FROM TEACHER
WHERE SALARY >60000;

Query 10: Find out student which are not register for any course.

SELECT STUDENTNO
FROM ENROLL
WHERE GRADE IS NULL;

Query 11: Gives the number of student in university

SELECT COUNT (*)


FROM STUDENT;

Query 12: Maximum salary of faculty.

SELECT MAX (SALARY)


FROM FACULTY;

Query 13: Gives the average salary of faculty whose salaries are more 40,000.

SELECT AVG (SALARY) AS ‘Average Salary’


FROM FACULY
WHERE SALARY >40000;

Query 14: Find out number student register for courses.

SELECT COUNT (DISTINCT STUDENTNO)


FROM ENROLLS;

Query 15: Find the names of student who are in COURSENO CEL747, and got GRADE
=F;

SELECT STUDENTNAME
FROM STUDENT s JOIN ENROLL e ON e.STUDENTNO = s.STUDENTNO
WHERE COURSENO = ‘CEL747’ AND GRADE = ‘F’;
Query 16: In which course did males’ student get ‘A’ grade?

SELECT STUDENTNAME, COURSENAME, GRADE


FROM STUDENT s JOIN ENROLL e ON e.STUDENTNO = s.STUDENTNO
JOIN COURSE c ON c.COURSENO = e.COURSENO
WHERE SEX= ‘M’ AND GRADE = ‘A’;

Query 17: List the name of all FACULTY ordered alphabetically descending by
department.

SELECT FACULTYNAME, DEPARTMENT


FROM FACULTY f JOIN DEPARTMENT d ON d.DNUMBER =f.DNO
ORDER BY DEPARTMENT DESC;

Query 18: How many students are there in Prof. Gosain GIS’s class?

SELECT COUNT (*)


FROM ENROLL e JOIN COURSE c ON c.COUSENO = e.ENROLLS
JOIN FACULTY f ON f.FACLUTYNO =c.FACULTYNO
WHERE TEACHERNAME = ‘Prof A. K. Gosain’ AND COURSENAME= ‘GIS’;

4 TUTORIAL
4.1 DESIGN A DATABASE

Design a database to keep track of the grades that students receive in their courses Current
Year Current semester. The following items to be included in the database:

1. Student id number (unique)


2. Student name,
3. Student department affiliation
4. Student hostel
5. Faculty id number (unique)
6. Faculty name
7. Faculty department affiliation
8. Course number (unique)
9. Number of credits offered for course,
10. Semester during which course is taught
11. Courses offered in the current semesters
12. Final grade
13. Hostel name
14. Department name

Add data fields wherever necessary to bring the clarity


Tasks

 Determine what your objects/entities will be


 Describe relationships between two objects
 Draw dependency diagram
 Define your table
 Define the ER model for this database. More specifically define:
i. The entity sets. For each entity set specify: attributes, primary key
ii. The relationships set. For each entity set specify: descriptive attributes,
primary key
iii. The ER diagram with: Key constraints, and participation constraints

4.2 QUERIES
Queries are a fundamental means of accessing and displaying data from tables. Queries can
access a single table or multiple tables.

Do Following Query

1. Find all the students belonging to CE who have obtained “A” and “B” grade in “CExxx”
subject. Result should have Student name, Registration Number, Hostel Name, Course
Name and Grade.
2. Find the Faculty belonging to CE and the courses they take. Result should have Faculty
ID, Faculty name, Department name, Course code, course name.
3. Find out the course floated in CE department and the faculty teaching the respected
course. Query should result in Course code, course name, semester, faculty ID, faculty
Name.
4. Which student from “xx” hostel has taken CE course? Should include student ID,
student name, Hostel name, Course name, Course code, faculty taking the course, grade
the student has obtained in this course.

You might also like