0% found this document useful (0 votes)
22 views5 pages

SQL Commands and Concepts Guide

The document provides an overview of SQL concepts including DDL, DML, and various SQL commands such as SELECT, INSERT, and JOIN. It explains the differences between various SQL clauses like WHERE and HAVING, as well as the definitions of keys such as Primary, Alternate, and Foreign keys. Additionally, it covers aggregate functions, constraints, and the distinctions between data types like CHAR and VARCHAR.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

SQL Commands and Concepts Guide

The document provides an overview of SQL concepts including DDL, DML, and various SQL commands such as SELECT, INSERT, and JOIN. It explains the differences between various SQL clauses like WHERE and HAVING, as well as the definitions of keys such as Primary, Alternate, and Foreign keys. Additionally, it covers aggregate functions, constraints, and the distinctions between data types like CHAR and VARCHAR.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

table.

For Example- create table, alter table, drop


table
What is the use of commit and rollback DML-Data manipulation language. Consist of
command in MySql commands used to modify the data of a table.
For Example- insert, delete, update
Commit : MySqlConnection.commit() method
sends a COMMIT statement to the MySql Consider the following Python code is written
server, committing the current transaction. to access the record of CODE passed to
function: Complete the missing statements:
def Search(eno):
Rollback: MySqlConnection.rollback reverts
#Assume basic setup import, connection
the changes made by the
and cursor is created
current transaction.
query="select * from emp where
empno=________".format(eno)
mycursor.execute(query)
Differentiate between WHERE and HAVING results = mycursor._________
clause. print(results)

WHERE clause is used to select particular Ans:


rows that satisfy a condition
{ } and fetchone()
whereas HAVING clause is used in connection
with the aggregate What do understand by an Alternate key? Give a
function, GROUP BY clause. suitable example to support your answer
For ex. – select * from student where marks > Those candidate keys which are not made the Primary
75; key are called the Alternate keys. Example : In Student
This statement shall display the records for all table with structure (Admno, Rollno, Name, Marks) If
the students who have Admno is made the Primary key, then Rollno will be the
scored more than 75 marks. Alternate key
On the contrary, the statement – select * from
student group by stream i) Name the package for connecting Python with
having marks > 75; shall display the records of MySQL database. ii) What is the purpose
all the students grouped of cursor object?
together on the basis of stream but only for i) import mysql.connector
those students who have ii) It is the object that helps to execute the SQL
scored marks more than 75. queries and facilitate
row by row processing of records in the resultset

Define Primary Key of a relation in SQL. Give


an Example using a dummy table How is equi-join different from
natural-join? Give example.
Primary Key- one or more attribute of a relation
used to uniquely identify each and every tuple Equi-join : It is a sql join where we use the equal sign
in the relation. For Example : In the below Table as the comparison
Student, RollNo can be the Primary Key operator while specifying the join condition. In this,
the common column from
both the tables will appear twice in the output.
Natural join : It is similar to Equi-join but only one
of the identical columns
exist in the output.
Example : select * from student, course where
course.cid = student.cid;
Differentiate between DDL and DML with one (Equi-join)
Example each Select * from student natural join course where
course.cid = student.cid;
(Natural join)
DDL- Data definition language. Consists of
commands used to modify the metadata of a
What do you mean by domain of an
attribute in DBMS? Explain with an
example.
DDL – Create, Alter, Drop
Domain of an attribute is the set of DML- Select, Insert, Update, Delete
values from which a value may come in
a
column. E.g. Domain of section field Differentiate between DELETE and DROP TABLE
may be (A,B,C,D). command.DROP command is used to drop a table
along with all the records stored in it whereas
Differentiate between fetchone()
and fetchmany() methods with DELETE command is used to delete all the records or
suitable some of the records from a table
without deleting the table
Examples.

fetchone() is used to retrieve one


record at a time but fetchmany(n) will What is Constraint ? Give example of any
fetch n two constraints.
records at a time from the table in the
form of a tuple. Constraints are the checking condition
which we apply on table to ensure the
What is the difference between correctness of data . example primary key,
CHAR & VARCHAR data types in
nut null, default, unique etc
SQL? Give an Example for each
CHAR is used to occupy fixed memory
irrespective of the actual values but Write the steps to perform an Insert
VARCHAR uses only that much memory query in database connectivity
which is used actually for the entered application.
values. Table ‘student’ values are
E.g. CHAR(10) will occupy always 10 rollno, name, age
bytes in memory no matter how many (1,’AMIT’,22)
characters are used in values. But
VARCHAR will uses only that much
import mysql.connector as mydb
bytes of
memory whose values are passed conn= mydb.connect(host=”localhost”,
user=”root”, passwd=”1234”)
cur=conn.cursor()
What do you understand by Foreign Key in a table? cur.execute(“INSERT INTO student
Give a suitable example of Foreign Key from a table values(1,’AMIT’,22);”)
containing some meaningful data. cur.commit()
A Foreign Key creates a link between tables. It
references the primary key in another table and links it.
Differentiate between Primary key and
For example, the DeptID in the Employee table is a Candidate key
foreign key
Primary key is an attribute or set of
attributes that uniquely identify the values
Differentiate between fetchone() and fetchall() and can
methods with suitable examples for each. appear as foreign key in another table..
Ans fetchall() fetches all the rows of a query result. An Candidate key is an attribute or set of
empty list is returned if there is no record to fetch the attributes that you can consider as a Primary
cursor. fetchone() method returns one row or a single
key.
record at a time. It will return None if no more
Define Candidate Key and Alternate Key
rows / records are available.
with suitable examples from a table
containing
some meaningful data. In the above table Employee, empno,aadhar_no,
A table may have more than one such attribute/group voter_id all are candidate key
of attributes that identifies a tuple 2
uniquely, all such attribute(s) are known as Candidate If we define empno as primary key then
Keys. remaining candidate keys will be alternate key.

Write the full forms of TCL and DDL. Write any two
commands of DDL in SQL.

TCL – Transaction Control Language

DDL – Data Definition Language

Any two out of CREATE, DROP, ALTER

Differentiate between fetchone() and


fetchall() methods with suitable
examples for each.

fetchall() fetches all the rows of a query result. An


empty list is returned if there is no record 2
to fetch the cursor.
fetchone() method returns one row or a single record
at a time. It will return None if no more
rows / records are available

Differentiate between an Attribute and a


Tuple in a Relational Database with
suitable example.

Attributes / Field: Columns of the table


(Relation) is called as attributes.
Tuple: Rows of the table (relation) is called as
a tuple (record)

Explain any two aggregate function of SQL


with suitable example
SUM() : Returns sum of the values of the
selected column
MAX() : Returns the largest values from the
selected column

A table may have more than one such


attribute/group of attributes that identifies a
tuple
uniquely, all such attribute(s) are known as
Candidate Keys.

All the candidate key except primary key are


called Alternate key. What is relation? What is the difference between a
tuple and an attribute?
Table: Employee (empno, aadhar_no, voter_id,
ename, deptno, sal, city) A relation is a two-dimensional table. It contains
number of rows (tuples) and columns (attributes). This
is the horizontal part of the relation. One row
represents one record of the relation. The rows of a Following is the correct statement :
relation are also called tuples.
SELECT * FROM employee WHERE grade IS NULL ;

What is SQL? What are the various subdivisions of


SQL?

SQL means Structured Query Language. It is the set of


commands that is recognized by all RDBMS. Data What is difference between working of the following
Definition Language (DDL) Data Manipulation Language functions? Count(*),Count (), Count (DISTINCT), Count
(DML) Data Control Language (DCL) (ALL)

. How do following constraint work? Count(*):-

Unique: This constraint ensures that no two rows have The COUNT(*) function returns the number of records
the same value in the specified columns. in a table: SELECT COUNT(*) FROM student;

For eg , CREATE TABLE employee (ecode integer NOT Count ():- The COUNT() function returns the number of
NULL UNIQUE, ename char(20),Sex char(2) ); values (NULL values will not be counted) of the specified
column: SELECT COUNT(name) FROM student;
Primary Key: Primary key does not allow NULL value
and Duplicate data in the column which is declared as Count (DISTINCT):- The COUNT(DISTINCT
Primary Key. column_name) function returns the number of distinct
values of the specified column: SELECT COUNT(DISTINCT
For eg , CREATE TABLE employee (ecode integer NOT city) FROM student;
NULL PRIMARY KEY, ename char(20),Sexchar(2) );
Count (ALL):- to count the number of non-null values in
Default: When a user does not enter a value for the column dept, i.e. counting repetitions too. SELECT
column, automatically the defined default value is COUNT(ALL) FROM student;
inserted in field. A column can have only one default
value.

For eg , CREATE TABLE employee (ecode integer NOT


NULL PRIMARY KEY, ename char(20), Sexchar(2), Grade
char(2) DEFAULT = ‘E1’ );

Check: This constraint limits values that can inserted What are views? When can a view be updated?
into a column of table. A view is, in essence, a virtual table. It does not
For eg , CREATE TABLE employee (ecode integer NOT physically exist. Rather, it is created by a query joining
NULL PRIMARY KEY, ename char(20),Sex char(2) , Grade one or more tables.
char(2) DEFAULT = ‘E1’, Gross decimal CHECK (gross > In following condition a view can be updated :
2000 ); If it has only one base table.
If select statement used in view contains group
by clause then we cant update view
Compare DISTINCT and ALL keywords when used with
SELECT command. To create a table DEPTO30 to hold the employee
numbers, names, jobs and salaries of employee in
DISTINCT keyword is used to restrict the duplicate rows
department with DeptNo = 30.
from the results of a SELECT statement. ALL keyword
CREATE TABLE DEPTP30 AS(SELECT EmpNo, EmpName,
retains the duplicate rows, by default ALL keyword is
Job, Sal FROM EMP WHERE DeptNo=30);
use by SELECT statement

What is wrong with the following statement? Write the Display data for all CLERKS who earn between 1000
corrected form of this query : and 2000.
SELECT * FROM emp WHERE((job LIKE 'clerk') AND (sal
SELECT * FROM employee WHERE grade = NULL ;
BETWEEN 1000 AND 2000));
Ans: Display data for all employees sorted by their
department, seniority and salary.
IS NULL should be used in place of = NULL.
SELECT * FROM emp ORDER BY deptno, hiredate, sal;
Write a SQL statement to list EmpNo, EmpName,
DeptNo, for all the employees. This information is
should be sorted on EmpName.
SELECT empno, empname,deptno FROM emp ORDER BY
empname;

Write SQL statement for : Find all the employees who


have no manager.
SELECT empname FROM emp WHERE mgr IS NULL;

To display DeptNo, Job, EmpName in reverse order of


salary from the EMP table.
SELECT deptno,job,empname FROM emp ORDER BY sal
DESC;
. List EmpName, Job, Sal for all the employees who
have a manager.
SELECT empname , job, salary from EMP WHERE mgr IS
NOT NULL;
List the minimum and maximum salary of each job
type.
SELECT job,MIN(sal),MAX(sal) FROM emp GROUP BY
job;
. Show the average salary for all departments with
more than 3 people for job.
SELECT AVG( sal ) FROM emp GROUP BY deptno
HAVING COUNT(job)>3;
Display only the jobs with maximum salary greater
than or equal to 3000.
SELECT job FROM emp GROUP BY job HAVING
MAX(salary)>=3000;

Find out number of employee having ‘Manager’ as


job.

SELECT COUNT(empname) FROM emp WHERE job LIKE


'Manager';
Create view Dept20 with EmpName and the Sal of
employees for dept 20.
CREATE VIEW dept20 as SELECT empname, sal FROM
emp WHERE deptno=20;

You might also like