Sunteți pe pagina 1din 12

CHAPTER -14

SQL COMMANDS
ONE MARK QUESTOINS
1. Expand SQL
Structured Query Language
2. Give the syntax for create command in sql
Create table table_name (column_name1
datatype, column_name2
column_name1 datatype,);

datatype,

3. what is drop command in sql


This is used to removes the table permanently from memory with all data
,constraints,indexes,constraints,
Drop table tablename;
4. Give the command to display all the details in the table
Select * from table table_name
5. What is update command?
Update command is used to update selected column value to update using where
condition other wise all the rows would be updated.
EX: Update table_name set column1=value1,column2=value2, .. .. column=valuen
where [condition]
6. What is commit command?
COMMIT to save the changes.
Two marks questions
1. classify Numeric and Character string data type in sql
A data type defines what kind of value a column can contain.
A. Numeric Data type
1. INT Integer numerical (no decimal). Precision 10
2. NUMERIC(p,s)
Exact numerical, precision p, scale s.
B. Floating Point Data type
1.FLOAT
Approximate numerical, mantissa precision 16
2.REAL
Approximate numerical, mantissa precision 7
C. Data types for storing a date or a date/time value in the database:
1. DATETIME
2. DATE
3. TIME
2. Classify various sql operators

CHAPTER -14
An operator is a reserved word or a character used primarily in an SQL
Operators are used in sql to specify condition in an sql
Arithmetic operators
Ex: + . -, *,
Comparison operators
Ex: <>,<,>,!=
Logical operators
Ex: ALL,AND,ANY,BETWEEN,EXISTS

Operators used to negate conditions

3. Which are the logical operators in sql


1. ALL- The ALL operator is used to compare a value to all values in another value
set.
2. AND- The AND operator allows the existence of multiple conditions in an SQL
statement's WHERE clause.
3. ANY-The ANY operator is used to compare a value to any applicable value in the
list according to the condition.
1. 4. BETWEEN- The BETWEEN operator is used to search for values that are
within a set of values, given the minimum value and the maximum value.
4. EXISTS-The EXISTS operator is used to search for the presence of a row in a
specified table that meets certain criteria.
2. 6. IN- The IN operator is used to compare a value to a list of literal values that
have been specified.
3. 7. LIKE - The LIKE operator is used to compare a value to similar values using
wildcard operators.
4. 8. NOT - The NOT operator reverses the meaning of the logical operator with
which it is used. Eg: NOT EXISTS, NOT BETWEEN, NOT IN, etc. This is a
negate operator.
5. 9. OR - The OR operator is used to combine multiple conditions in an SQL
statement's WHERE clause.
6. 10. IS NULL - The NULL operator is used to compare a value with a NULL value.
7. 11. UNIQUE -The UNIQUE operator searches every row of a specified table for
uniqueness (no duplicates).
4. How do you modify the column name and width for existing table
ALTER TABLE command is used to add, delete or modify columns in an existing table.
Syntax:
ALTER TABLE "table_name" RENAME COLUMN "column 1" TO "column 2";
Ex: ALTER TABLE STUDENT RENAME ADDRESS TO ADDRS;
Syntax:
ALTER TABLE table_name MODIFY COLUMN column_name datatype;
Ex: ALTER TABLE STUDENT MODIFY ADDRESS CHAR(50);

CHAPTER -14
5. write the syntax for distinct command in sql
The SQL DISTINCT keyword is used in conjunction with SELECT statement to
eliminate all the duplicate records and fetching only unique records.
Syntax:
SELECT DISTINCT column1, column2,.....columnN FROM table_name WHERE
[condition]
Ex: SELECT DISTINCT SALARY FROM CUSTOMERS ORDER BY SALARY;
6. What is the use of null value
NULL is the term used to represent a missing value
It is very important to understand that a NULL value is different than a zero
value or a field that contains spaces.
A NULL value in a table is a value in a field that appears to be blank.
7. What is create view command
1. A VIEW is a virtual table, through which a selective portion of the data from one
or more tables can be seen.
2. Views do not contain data of their own.
3. Operations on a view like INSERT, UPDATE, DELETE affects the data in the
original table upon which the view is based.
The Syntax to create a sql view is
CREATE VIEW view_name AS SELECT column_list FROM table_name
[WHERE condition];
Ex: CREATE VIEW STUDENT_VIEW AS SELECT name, age FROM STUDENT;
8. what is dual table
This is a single row and single column dummy table.
This is used to perform mathematical calculations without using a table.
Three marks questios
1. Explain the features of sql
* SQL is an ANSI and ISO standard computer language for creating and manipulating
databases.
* SQL allows the user to create, update, delete, and retrieve data from a database.
*SQL is very simple and easy to learn.
*SQL works with database programs like DB2, Oracle, MS Access, Sybase, MS SQL
Sever etc.
* SQL is the standard language for relation database system.
2. List the components of sql architecture

SQL Query

CHAPTER -14
Query Language Processor
Optimizer)
DBMS Engine
Physical Database

(Parser+

3. Explain DDL commands with example


DDL - Data Definition Language:
1.CREATE TABLE
1. The SQL CREATE TABLE statement is used to create a new table.
2. CREATE TABLE is the keyword telling the database system to create a new
table.
3. In brackets comes the list defining each column in the table and what sort of
data type it is.
Basic syntax of CREATE TABLE statement is as follows:
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3
datatype, ..... columnN datatype, PRIMARY KEY ( one or more columns ) );
Ex: CREATE TABLE STUDENT(ID
INT NOT NULL, NAME VARCHAR (20)
NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , PRIMARY KEY (ID) );

NOT

2.ALTER TABLE
The SQL ALTER TABLE command is used to add, delete or modify columns in an
existing table.
The ALTER command is used to perform the following functions.
1) Add, drop, modify table columns
2) Add and drop constraints
3) Enable and Disable constraints on a existing table.
The basic syntax of ALTER TABLE to change the DATA TYPE of a column in a table is
as follows:
ALTER TABLE table_name MODIFY COLUMN column_name datatype;
Ex:To modify the column ADDRESS CHAR (25) in the student table, the query would
be like

CHAPTER -14
ALTER TABLE STUDENT MODIFY ADDRESS CHAR(50);
3. DROP TABLE
The SQL DROP TABLE statement is used to remove a table definition and all data,
indexes, triggers, constraints, and permission specifications for that table.
Basic syntax of DROP TABLE statement is as follows:
DROP TABLE table_name;
Ex: DROP TABLE STUDENT;
4. Explain DML commands with example
1. INSERT INTO
The SQL INSERT INTO Statement is used to add new rows of data to a table in the
database.
Syntax: There are two basic syntaxes of INSERT INTO statement as follows:
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)]
VALUES
(value1, value2, value3,...valueN);
Here, column1, column2,...columnN are the names of the columns in the table into
which you want to insert data.
OR
INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);
Ex: INSERT INTO STUDENT(ID,NAME,AGE,ADDRESS) VALUES (1, 'Ramesh', 32,
'Bangalore');
INSERT INTO STUDENT VALUES (1, 'Ramesh', 32, 'Bangalore');

2.UPDATE
1. The SQL UPDATE Query is used to modify the existing records in a table.
2. You can use WHERE clause with UPDATE query to update selected rows
otherwise all the rows would be affected.
Syntax: The basic syntax of UPDATE query with WHERE clause is as follows:
UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
You can combine N number of conditions using AND or OR operators.
Ex: UPDATE STUDENT SET ADDRESS = 'Pune' WHERE ID = 1;
3. DELETE
1. The SQL DELETE Query is used to delete the existing records from a table.

CHAPTER -14
2. use WHERE clause with DELETE query to delete selected rows, otherwise all
the records would be deleted
3. If you want to DELETE all the records from STUDENT table, you do not need to
use WHERE clause
Syntax of DELETE query with WHERE clause is as follows:
DELETE FROM table_nameWHERE [condition];
Ex: DELETE FROM STUDENT WHERE ID = 6;
DELETE query would be as follows:
Ex: DELETE FROM STUDENT;
5. Explain with an example Boolean expression in sql
1. An expression is a combination of one or more values, operators, and SQL
functions that evaluate to a value.
2. SQL Boolean Expressions fetch the data on the basis of matching single value
syntax:SELECT column1, column2, columnN
VALUE MATCHTING EXPRESSION;

FROM table_name WHERE SINGLE

SELECT * FROM STUDENT WHERE NAME = RAMESH;


6. Explain AND operator using where in sql
The AND operator allows the existence of multiple conditions in an SQL statement's
WHERE clause.
SELECT * FROM STUDENT WHERE AGE >= 25 AND NAME= RAMESH;

7. List the built in functions associated with group functions in sql


1) COUNT Function - The SQL COUNT aggregate function is used to count the
number of rows in a database table.
2) MAX Function - The SQL MAX aggregate function allows us to select the
highest (maximum) value for a certain column.
3) MIN Function - The SQL MIN aggregate function allows us to select the lowest
(minimum) value for a certain column.
4) AVG Function - The SQL AVG aggregate function selects the average value for
certain table column.
5) SUM Function - The SQL SUM aggregate function allows selecting the total for
a numeric column.

CHAPTER -14
6) SQRT Functions - This is used to generate a square root of a given number.
7) RAND Function - This is used to generate a random number using SQL
command.
8) CONCAT Function - This is used to concatenate any string inside any SQL
command.
9) Numeric Functions - Complete list of SQL functions required to manipulate
numbers in SQL.
10) String Functions - Complete list of SQL functions required to manipulate
strings in SQL.
8. What is the use of join command?
1. Joins clause is used to combine records from two or more tables in a database.
2. A JOIN is a means for combining fields from two tables by using values common
to each.
3. Join is performed in the WHERE clause. Several operators can be used to join
tables, such as =, <, >, <>, <=, >=, !=, BETWEEN, LIKE, and NOT; they can all
be used to join tables.
Ex:
Join two tables STDENT and DETAILS in our SELECT statement as follows:
SELECT ID, NAME, AGE FROM STUDENT
WHERE STUDENT.ID = DETAILS.STUDENT_ID;
There are different types of joins available in SQL:
INNER JOIN: returns rows when there is a match in both tables.
LEFT JOIN: returns all rows from the left table, even if there are no matches in
the right table.
RIGHT JOIN: returns all rows from the right table, even if there are no matches
in the left table.

FULL JOIN: returns rows when there is a match in one of the tables.
SELF JOIN: is used to join a table to itself as if the table were two tables,
temporarily renaming at least one table in the SQL statement.
CARTESIAN JOIN: returns the Cartesian product of the sets of records from the
two or more joined tables.

9. What are privileges and roles


Privileges: is the access right or privilege granted to the user on database object .
Some of the access rights are ALL, EXECUTE, and SELECT.
GRANT privilege_name { access right}
ON object_name {Name of DB, TABLE, VIEW, STORED PROC and SEQUENCE. }
TO {user_name |PUBLIC |role_name}
[WITH GRANT OPTION]; { allows a user to grant access rights to other users.}

CHAPTER -14
Ex: GRANT SELECT ON STUDENT TO user1;
There are two types of privileges.
1) System privileges - This allows the user to CREATE, ALTER, or DROP database
objects.
2) Object privileges - This allows the user to EXECUTE, SELECT, INSERT, UPDATE, or
DELETE data from database objects to which the privileges apply.
Roles: Roles are a collection of privileges or access rights.
It's easier to GRANT or REVOKE privileges to the users through a role rather than
assigning a privilege directly to every user.
The Syntax to create a role is:
CREATE ROLE role_name [IDENTIFIED BY password];
For Example: To create a role called "developer" with password as "pwd",the code will
be as follows
CREATE ROLE developer [IDENTIFIED BY pwd];
Ex: Some of the privileges granted to the system roles are as given below:
DBA ALL SYSTEM PRIVILEGES
DEVELOPER CREATE VIEW,INSERT VALUES
10. Classify various built in functions in sql
There are two types of functions
1)
Single Row Functions: Single row or Scalar functions return a value for
every row that is processed in a query.
2)
Group Functions: These functions group the rows of data based on the
values returned by the query.
There are four types of single row functions. They are:
1) Numeric Functions: These are functions that accept numeric input and return
numeric values.
Ex: CEIL(x),FLOOR(x)ROUND(x,y)
2) Character or Text Functions: These are functions that accept character
input and can return both character and number values.
Ex:LOWER(string_value).UPPER(string_value),TRIM(trim_textFROMstring_val
ue)
3) Date Functions: These are functions that take values that are of datatype
DATE as input and return values of datatype DATE, except for the
MONTHS_BETWEEN function, which returns a number.
Ex:SYSDATE,LAST_DAY(x)

CHAPTER -14
4) Conversion Functions:
These are functions that help us to convert a value in one form to another
form.
For Example: a null value into an actual value, or a value from one datatype
to another datatype like NVL, TO_CHAR, TO_NUMBER, TO_DATE etc.
The Group Functions are:
1) COUNT Function - The SQL COUNT aggregate function is used to count the
number of rows in a database table.
2) MAX Function - The SQL MAX aggregate function allows us to select the highest
(maximum) value for a certain column.
3) MIN Function - The SQL MIN aggregate function allows us to select the lowest
(minimum) value for a certain column.
4) AVG Function - The SQL AVG aggregate function selects the average value for
certain table column.
5) SUM Function - The SQL SUM aggregate function allows selecting the total for a
numeric column.
Five marks questions
1. Explain sql constraints with example
1. Constraints are the rules enforced on data columns on table.
2. These are used to limit the type of data that can go into a table.
3. This gives the accuracy and reliability of the data in the database.
Following are commonly used constraints available in SQL.
1. NOT NULL Constraint: Ensures that a column cannot have NULL value.
2. DEFAULT Constraint: Provides a default value for a column when none is
specified.
3. UNIQUE Constraint: Ensures that all values in a column are different.
4. PRIMARY Key: Uniquely identified each rows/records in a database table.
5. FOREIGN Key: Uniquely identified a rows/records in any another database
table.
6. CHECK Constraint: The CHECK constraint ensures that all values in a column
satisfy certain conditions.
7. INDEX: Use to create and retrieve data from the database very quickly.

Primary key Constraints


1. A primary key is a field in a table which uniquely identifies each row/record in
a database table.
2. A primary key column cannot have NULL values.
Here is the syntax to define ID attribute as a primary key in a STUDENT table.

CHAPTER -14
CREATE TABLE STUDENT(
NOT NULL,
AGE INT
PRIMARY KEY (ID) );

ID INT
NOT NULL,

NOT NULL,
NAME VARCHAR (20)
ADDRESS CHAR (25) ,

Foreign key/ referencing key.:


1. A foreign key is a key used to link two tables together.
2. This is sometimes called a referencing key.
3. The relationship between 2 tables matches the Primary Key in one of the tables
with a Foreign Key in the second table.
CREATE TABLE STUDENT(
NOT NULL,
AGE INT
PRIMARY KEY (ID) );

ID INT
NOT NULL,

NOT NULL,
NAME VARCHAR (20)
ADDRESS CHAR (25) ,

CREATE TABLE DETAILS (


ID
INT
NOT NULL,
DOB
DATETIME,
CONSTRAINT STUDENT_ID INT references CUSTOMERS(ID),
AMOUNT
double, PRIMARY KEY (ID) );
NOT NULL Constraint:
1. Ensures that a column cannot have NULL value.
2. A NULL is not the same as no data, rather, it represents unknown data.
Syntax: [CONSTRAINT constraint name] NOT NULL
Ex: CREATE TABLE STUDENT (
ID INT
NOT NULL,
VARCHAR (20)
NOT NULL,
AGE INT
NOT NULL,
(25) ,
PRIMARY KEY (ID) );

NAME
ADDRESS CHAR

UNIQUE Constraint:
1. Ensures that all values in a column are different.
2. The UNIQUE Constraint prevents two records from having identical values in a
particular column.
Syntax: [CONSTRAINT constraint name] UNIQUE
Ex: CREATE TABLE STUDENT( ID INT NOT NULL,NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL UNIQUE, ADDRESS CHAR (25) , PRIMARY KEY (ID) );
Here, AGE column is set to UNIQUE, so that you cannot have two records with same
age:
CHECK Constraint:
1. The CHECK constraint ensures that all values in a column satisfy certain
conditions.
2. This constraint defines a business rule on a column or group of column

CHAPTER -14
Syntax: [CONSTRAINT constraint name] CHECK(Condition)
Ex: CREATE TABLE STUDENT(
ID INT
(20)
NOT NULL,
AGE INT
ADDRESS CHAR (25) ,
PRIMARY KEY (ID) );

NOT NULL,
NAME VARCHAR
NOT NULL CHECK (AGE >= 18),

2. Write the differences bt order by and group by with example


SQL ORDER BY
1. The SQL ORDER BY clause is used to sort the data in ascending or descending
order, based on one or more columns.
The basic syntax of ORDER BY clause is as follows:
SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1,
column2, .. columnN] [ASC | DESC];
EX: SELECT * FROM STUDENT

ORDER BY NAME, AGE;

GROUP BY clause
1. The SQL GROUP BY clause is used in collaboration with the SELECT statement
to arrange identical data into groups.
2. The GROUP BY clause must follow the conditions in the WHERE clause and
must precede the ORDER BY clause if one is used.
The basic syntax of GROUP BY clause is given below.
SELECT column1, column2 FROM table_name WHERE [ conditions ] GROUP BY
column1, column2 ORDER BY column1, column2
Ex: SELECT NAME, SUM(SALARY) FROM CUSTOMERS
ID | NAME
| AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 4 | Chaitali | 23 | Mumbai | 6500.00 |
| 5 | Hardik | 25 | Bhopal | 8500.00 |
| 3 | kaushik | 27 | Kota
| 2000.00 |
| 2 | Khilan | 28 | Delhi
| 1500.00 |
| 6 | Komal | 29 | MP
| 4500.00 |
| 7 | Muffy | 30 | Indore | 10000.00 |
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
+----+----------+-----+-----------+----------+

GROUP BY NAME;

3. Explain with example to create details of employees and give the min and max
in the salary domain
Advantages of SQL:
* High Speed:

CHAPTER -14
SQL Queries can be used to retrieve large amounts of records from a database quickly
and efficiently.
* Well Defined Standards Exist: SQL databases use long-established standard, which
is being adopted by ANSI & ISO. Non-SQL databases do not adhere to any clear
standard.
* No Coding Required: Using standard SQL it is easier to manage database
systems without having to write substantial amount of code.
* Emergence of ORDBMS: Previously SQL databases were synonymous with
relational database. With the emergence of Object Oriented DBMS, object storage
capabilities are extended to relational databases.
SELECT
SQL SELECT statement is used to fetch the data from a database table which returns
data in the form of result table.
The basic syntax of SELECT statement is as follows:
SELECT column1, column2, columnN FROM table_name;
Here, column1, column2...are the fields of a table whose values you want to fetch.
Ex: SELECT ID, NAME FROM STUDENT;
If you want to fetch all the fields available in the field, then you can use the following
syntax:SELECT * FROM table_name;
Ex: SELECT * FROM STUDENT;

S-ar putea să vă placă și