Professional Documents
Culture Documents
Saurabh Tariyal Sec:J 1918677
Saurabh Tariyal Sec:J 1918677
Assignment 1.
Q1. Write various steps to create SQL user step by step.
Assignment 2
Enter password:
3. View the user name (Make sure it’s your user name: eg CSA23 etc)
show user
Desc student;
Desc student;
Rollback;
Saurabh Tariyal Sec:J 1918677
Desc student;
23. Complete all the blank records of your own and add 5 More records (Total = 10).
Commit;
Desc student;
Set pagesize 7;
Desc student;
44. Make a duplicate copy of Student table with the name “Duplicate”
Rollback;
3.View the user name, make sure it’s your login name.
show user;
Saurabh Tariyal Sec:J 1918677
EmpCode Number
Name varchar2 6 (Six)
Salary Number
Desc employee;
7.Insert the following data into the table using any method:
10. See the structure of table and note the size of name column
Desc employee;
16. Reduce the size of name column to 5. (Note the output even if it is an error)
alter table employee modify(name varchar2(5));
Commit;
Saurabh Tariyal Sec:J 1918677
Saurabh Tariyal Sec:J 1918677
18. Execute the following SQL queries and note the result:
a) Select name from employee;
20. Make a copy of employee table as “NewEmp” table with all records.
create table newemp as select * from employee;
Saurabh Tariyal Sec:J 1918677
22. See the records of all 3 table (Employee, NewEmp and Emp2).
select * from employee;
select * from newemp;
select * from emp2;
Saurabh Tariyal Sec:J 1918677
23. See the structure of all 3 table (Employee, NewEmp and Emp2).
Desc employee;
Desc newemp;
Desc emp2;
24. Create a new table “MyTable” from employee table with only Name and Salary
columns.
create table mytable as select name, salary from employee;
28. Drop the columns “name” and “empcode” from NewEmp table.
alter table newemp drop(name,empcode);
30. Truncate the “Mytable” table. (do not use delete command).
truncate table mytable;
Saurabh Tariyal Sec:J 1918677
32. Rollback.
Rollback;
33. Again See the records of “MyTable” table. (What is your observation?)
select * from mytable;
36. Rollback.
Rollback;
Saurabh Tariyal Sec:J 1918677
Saurabh Tariyal Sec:J 1918677
37. Again See the records of “MyTable” table. (What is your observation?)
select * from mytable;
Assignment 3
6.Show records of all students having age greater than 25 & living in Dehradun.
Saurabh Tariyal Sec:J 1918677
7.Show the list of all cities (names of cities should not be repeated)
in uppercase.
14 Aviral 319143 29
15 Gwaliar 313149 25
11.A. Assign Roll No. “13” to Manish.
11.B. Assign the name “Abhijeet” to Roll No. 15
11.C. Increase all age by 3 years.
11.D. Set the age of all students living in Meerut to “25”.
12. Add a new column named “Balance”, type number to the Students table.
Saurabh Tariyal Sec:J 1918677
13. Set the balance of all students to Rs. 20,000/-. See All records. Commit.
Saurabh Tariyal Sec:J 1918677
14. Increase the balance by Rs. 500/- for all Bombay & Delhi students. See All records.
Commit.
17.Create a new table “student2” as a copy of Students with all rows from Students table
Course: B Tech
2. See and note the structure of above created table using “describe”.
3. See the contents of “User_Constraints” table and note the constraint number and type
Saurabh Tariyal Sec:J 1918677
4. Insert the following data in the above table (In case of Error , Correct the data yourself)
Ans:
Output:
Saurabh Tariyal Sec:J 1918677
6. See and note the structure of above created table using “describe”. (Note structure carefully)
Output:
:
Saurabh Tariyal Sec:J 1918677
8. See the contents of “User_Constraints” table and note the constraint number and type
Output:
10. WAQ (write a ans) to create an “Account4” table with following constraints:
11. See the contents of “User_Constraints” table and note the constraint number and type
12. Drop the Primary Key & Unique Constraint from Account4 Table.
13. WAQ (write a ans) to create an “Account5” table without any constraints:
:
Saurabh Tariyal Sec:J 1918677
16. Add the primary key constraint for AccountNo in Account5 Table
(If an error comes, note the error and write the reason)
17. In case of error in above ques, rectify the above table contents and repeat above Question
18. See the contents of “User_Constraints” table and search the name of Account5 table in it.
20. Add Check constraint in Account5 Table for Balance field. ( Amount > 1500)
21. In case of error in above ques, rectify the above table contents and repeat above Question
Assignment 4
PART – A
NUMERIC FUNCTIONS:
SQL> select round(15.6789)from dual;
MATH FUNCTIONS:
SQL> select abs(45) from dual;
Practical : PART – B
Q1. What are Single Row and Multiple Row Functions in Oracle SQL. Explain in detail.
Ans.
Single Row functions - Single row functions are the one who work on single row and return
one output per row. For example, length and case conversion functions are single row
functions.
Single row functions can be character functions, numeric functions, date functions, and
conversion functions. Single-row functions include TO_CHAR, TO_DATE, UPPER,
LOWER etcSingle-row functions can be used also in the SET clause of and UPDATE
command. Single-row functions cannot be used in a HAVING clause.
Multiple Row functions - Multiple row functions work upon group of rows and return one
result for the complete set of rows. They are also known as Group Functions. This is a
powerful feature because it allows you to generate subtotals, sums and averages within the
SQL that is retrieving the data. For now, we will apply the functions to all the rows we
return. In the next chapter, we will break up our returning rows into groups and apply the
functions to each of the groups independently.
INSTR- Tests whether a given character occurs in the given string or not. If the character
occurs in the string then returns the first position of its occurrence otherwise returns 0.
REPLACE- Replaces a given set of characters in a string with another set of characters.
INSTR- Tests whether a given character occurs in the given string or not. If the character
occurs in the string then returns the first position of its occurrence otherwise returns 0.
Saurabh Tariyal Sec:J 1918677
Number Functions –
Addition-- The addition operator (+).
Subtraction- The subtraction operator (-).
Multiplication- The multiplication operator (*).
Division- The division operator (/).
ABS- Returns the absolute value of n.If n is 0 or a positive integer, returns n.
CEIL- Returns the smallest integer value not less than n.
EXP- Exponentiation, where the base is e.Returns the value of e (the base of natural
logarithms) raised to the power n.
LOG- Logarithm. log(n, m) takes two arguments, where n is the base, and m is the
value you are taking the logarithm of.
MOD- Modulo. Returns the remainder of n divided by m.
ROUND- Returns a number rounded to the specified decimal place.
The unary (one argument) version takes only one argument (the number to be
rounded) and drops the decimal (non-integral) portion of the input.
Date Functions-
NOW(): Returns the current date and time.
CURDATE(): Returns the current date.
CURTIME(): Returns the current time.
DATE(): Extracts the date part of a date or date/time expression.
EXTRACT(): Returns a single part of a date/time.
DATE_ADD() : Adds a specified time interval to a date
DATE_SUB(): Subtracts a specified time interval from a date. Syntax for
DATE_SUB is same as DATE_ADD just the difference is that DATE_SUB is used to
subtract a given interval of date.
DATEDIFF(): Returns the number of days between two dates.
DATE_FORMAT(): Displays date/time data in different formats
Saurabh Tariyal Sec:J 1918677
Conversions Functions-
TO_CHAR function-used to typecast a numeric or date input to character type with a
format model
TO_NUMBER function-converts a character value to a numeric datatype. If the string
being converted contains nonnumeric characters, the function returns an error.
TO_DATE function-The function takes character values as input and returns
formatted date equivalent of the same. it allows users to enter a date in any format,
and then it converts the entry into the default format used by Oracle 11g.
NVL : In SQL, NVL() converts a null value to an actual value. Data types that can be
used are date, character and number. Data type must match with each other i.e.
expr1 and expr2 must of same data type.
Syntax –
NVL (expr1, expr2)
expr1 is the source value or expression that may contain a null.
expr2 is the target value for converting the null.
NVL2(expr1, expr2, expr3) : The NVL2 function examines the first expression. If the
first expression is not null, then the NVL2 function returns the second expression. If
the first expression is null, then the third expression is returned i.e. If expr1 is not null,
NVL2 returns expr2. If expr1 is null, NVL2 returns expr3.
Syntax –
NVL2 (expr1, expr2, expr3)
expr1 is the source value or expression that may contain null
expr2 is the value returned if expr1 is not null
expr3 is the value returned if expr1 is null
Saurabh Tariyal Sec:J 1918677
CASE- The CASE statement goes through conditions and returns a value when the
first condition is met (like an if-then-else statement). So, once a condition is true, it
will stop reading and return the result.
CASE Syntax- CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
e.g- SELECT OrderID, Quantity,
CASE
WHEN Quantity > 30 THEN 'The quantity is greater than 30'
WHEN Quantity = 30 THEN 'The quantity is 30'
ELSE 'The quantity is under 30'
END AS QuantityText
FROM OrderDetails;
Assignment 6
1.Write a SQL query to find the maximum order (purchase) amount for each customer. The
customer ID should be in the range 3002 and 3007(Begin and end values are included.). Filter
the rows for maximum order (purchase) amount is higher than 1000. Return customer id and
maximum purchase amount.
Ans.
SELECT customer_id,MAX(purch_amt)
FROM orders
WHERE customer_id BETWEEN 3002 and 3007
GROUP BY customer_id;
Output:
2.Write a SQL query to find the maximum order (purchase) amount by the combination of each
customer and order date. Filter the rows for maximum order (purchase) amount is either 2000,
3000, 5760, 6000. Return customer id, order date and maximum purchase amount.
Ans.
SELECT customer_id,MAX(purch_amt)
FROM orders
WHERE customer_id BETWEEN 3002 and 3007
GROUP BY customer_id
HAVING MAX(purch_amt)>1000;
3. Write a SQL query to find the maximum order (purchase) amount in the range 2000, 6000
(Begin and end values are included.) by combination of each customer and order date. Return
customer id, order date and maximum purchase amount.
Ans.
SELECT customer_id,ord_date,MAX(purch_amt)
FROM orders
Saurabh Tariyal Sec:J 1918677
GROUP BY customer_id,ord_date
HAVING MAX(purch_amt) BETWEEN 2000 AND 6000;
4. Write a SQL query to find highest order (purchase) amount by each customer in a particular
order date. Filter the result by highest order (purchase) amount above 2000.00. Return
customer id, order date and maximum purchase amount.
Ans.
SELECT customer_id,ord_date,MAX(purch_amt)
FROM orders
GROUP BY customer_id,ord_date
HAVING MAX(purch_amt)>2000.00;
Saurabh Tariyal Sec:J 1918677
5. Write a SQL query to find the highest purchase amount on '2012-08-17' by each salesperson.
Return salesperson ID, purchase amount.
Ans.
SELECT salesman_id,MAX(purch_amt)
FROM orders
WHERE ord_date = '2012-08-17'
GROUP BY salesman_id;
Join
1. Write a SQL query to find the salesperson and customer who belongs to same city.
Return Salesman, cust_name and city.
Ans.
SELECT salesman.name AS "Salesman",
customer.cust_name, customer.city
FROM salesman,customer
WHERE salesman.city=customer.city;
Saurabh Tariyal Sec:J 1918677
2. Write a SQL query to find the salesperson(s) and the customer(s) he handle. Return
Customer Name, city, Salesman, commission.
Ans.
SELECT a.cust_name AS "Customer Name",
a.city, b.name AS "Salesman", b.commission
FROM customer a
INNER JOIN salesman b
ON a.salesman_id=b.salesman_id;
3. Write a SQL query to find those salespersons do not live in the same city where their
customers live and received a commission from the company more than 12%. Return
Customer Name, customer city, Salesman, salesman city, commission.
Ans.
SELECT a.cust_name AS "Customer Name",
a.city, b.name AS "Salesman", b.commission
FROM customer a
INNER JOIN salesman b
ON a.salesman_id=b.salesman_id
WHERE b.commission>.12;
Saurabh Tariyal Sec:J 1918677
4.Write a SQL query to display the cust_name, customer city, grade, Salesman, salesman city.
The result should be ordered by ascending on customer_id.
SELECT a.cust_name,a.city,a.grade,
b.name AS "Salesman",b.city
FROM customer a
LEFT JOIN salesman b
ON a.salesman_id=b.salesman_id
order by a.customer_id;
5.Write a SQL statement to make a list in ascending order for the salesmen who works
either for one or more customer or not yet join under any of the customers.
Ans.
SELECT a.cust_name,a.city,a.grade,
b.name AS "Salesman", b.city
FROM customer a
RIGHT OUTER JOIN salesman b
ON b.salesman_id=a.salesman_id
ORDER BY b.salesman_id;