0% found this document useful (0 votes)
69 views7 pages

03 Exp5 Abhishek

This document discusses performing simple SQL queries and string manipulation operations. It shows how to create tables, insert values, perform queries to retrieve data, calculate averages and sums, and use wildcard patterns for searching. Examples demonstrate inserting records, selecting rows that match criteria, finding maximum/minimum values, and joining data from multiple tables. String patterns can be matched using wildcards like % and _ to search for partial or specific matches.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views7 pages

03 Exp5 Abhishek

This document discusses performing simple SQL queries and string manipulation operations. It shows how to create tables, insert values, perform queries to retrieve data, calculate averages and sums, and use wildcard patterns for searching. Examples demonstrate inserting records, selecting rows that match criteria, finding maximum/minimum values, and joining data from multiple tables. String patterns can be matched using wildcards like % and _ to search for partial or specific matches.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

EXP.5 Perform Simple queries, string manipulation operations.

Aim:- Perform Simple queries, string manipulation operations.

Theory:

Q1. Create a table route_header with given attributes

SQL> create table route_header


2 (
3 rid int,
4 rno int,
5 catcode int,
6 origin varchar(20),
7 dest varchar(20),
8 fare int,
9 dist int,
10 capacity int,
11 primary key (rid)
12 );

Table created.

Q2. Create a table ticket_details with given attributes

SQL> create table ticket_details


2 (
3 tikno int,
4 name varchar(20),
5 age int,
6 fare int,
7 gender char,
8 primary key (tikno)
9 );
Table created.

Q3. Create a table ticket_header with given attributes

SQL> create table ticket_header


2 (
3 tikno int,
4 ttime int,
5 dot date,
6 origin varchar(20),
7 dest varchar(20),
8 bplace varchar(20),
9 adult int,
10 child int,
11 tfare int,
12 rid int,
13 fid int,
14 primary key (fid)
15 );

Table created.

Q4. Insert values into the respective tables & display them

SQL> insert into route_header


values(1,1,1,'Mumbai','Delhi',800,300,1000); 1 row created.

SQL> insert into route_header


values(2,1,2,'Pune','Bangalore',550,175,900); 1 row created.

SQL> insert into route_header


values(3,2,2,'Madras','Goa',650,350,1200); 1 row created.

SQL> insert into route_header


values(4,3,1,'Kolkata','Hyderabad',800,300,1000); 1 row created.

SQL> select *from route_header;

RID RNO CATCODE ORIGIN DEST FARE DIST CAPACITY


------- ------- ---------- ----------- --------- ------- ------- -------------
1 1 1 Mumbai Delhi 800 300 1000
2 1 2 Pune Bangalore 550 175 900
3 2 2 Madras Goa 650 350 1200
4 3 1 Kolkata Hyderabad 800 300 1000

SQL> insert into ticket_details


values(25,'Collin',20,800,'M'); 1 row created.
SQL> insert into ticket_details
values(48,'Melwin',19,550,'M'); 1 row created.

SQL> insert into ticket_details


values(12,'Royston',19,650,'M'); 1 row created.

SQL> insert into ticket_details values(30,'Snehal',20,800,'F');


1 row created.

SQL> select *from ticket_details;

TIKNO NAME AGE FARE G


------- ------------- -------- ---------- -
25 Collin 20 800 M
48 Melwin 19 550 M
12 Royston 19 650 M
30 Snehal 20 800 F

SQL> insert into ticket_header values(25, 46,'28-mar-


2008','Mumbai','Delhi','Dadar', 20,40,48000,1,1); 1 row created.

SQL> insert into ticket_header values(48,38,'26-sep-


2008','Pune','Bangalore','Nagpur', 30,10,22000,2,2); 1 row created.

SQL> insert into ticket_header values(12,40,'19-aug-


2008','Madras','Goa','Velankini', 30,20,32500,3,3); 1 row created.

SQL> insert into ticket_header values(30,30,'16-apr-


2008','Kolkata','Hyderabad', 'Howrah',20,20,32000,4,4); 1 row created.

SQL> select *from ticket_header;

TIKNO TTIME DOT ORIGIN DEST BPLACE


------- ------- ----- --------- ------- --------
25 46 28-MAR-08 Mumbai Delhi Dadar
48 38 26-SEP-08 Pune Bangalore Nagpur
12 40 19-AUG-08 Madras Goa Velankini
30 30 16-APR-08 Kolkata Hyderabad Howrah

ADULT CHILD TFARE RID FID


------- -------- -------- --- ---
20 40 48000 1 1
30 10 22000 2 2
30 20 32500 3 3
20 20 32000 4 4

Q5. Give the average of total fare from ticket_header

SQL> select avg(tfare) from ticket_header ;

AVG(TFARE)
------------------
33625

Q6. Find out the highest fare from ticket_details

SQL> select tikno,fare from ticket_details where fare=(select max(fare) from ticket_details);

TIKNO FARE
-------- ----------
25 800
30 800

Q7. Give the min distance from route_header

SQL> select rid,rno,dist from route_header where dist=(select min(dist) from route_header);

RID RNO DIST


------- -------- ---------
- 2 1 175

Q8. Give the total collection of fare from ticket_details

SQL> select sum(fare) from ticket_details;

SUM(FARE)
-----------------
2800

Q9. Give the total no of people who have travelled more than 36hrs.
Group by ticket no
SQL> select tikno,sum(adult+child) from ticket_header where ttime>=36 group by tikno;

TIKNO SUM(ADULT+CHILD)
---------- -----------------------------
12 50
25 60
48 40

Q10. Display system date in the format mentioned as "DDth MM


YYYY"

SQL> select to_char(sysdate,'ddth month yyyy') from dual;

TO_CHAR(SYSDATE,'DD
----------------------------------
26th march 2008

SQL pattern matching allows you to search for patterns in data if you don't know the exact word
or phrase you are seeking. This kind of SQL query uses wildcard characters to match a pattern,
rather than specifying it exactly. For example, you can use the wildcard "C%" to match any
string beginning with a capital C.

Using the LIKE Operator


To use a wildcard expression in an SQL query, use the LIKE operator in a WHERE clause, and
enclose the pattern within single quotation marks.

Using the % Wildcard to Perform a Simple Search


To search for any employee in your database with a last name beginning with the letter C, use the
following Transact-SQL statement:

SELECT *
FROM employees
WHERE last_name LIKE 'C%'

Omitting Patterns Using the NOT Keyword


Use the NOT keyword to select records that don't match the pattern. For example, this query
returns all records whose name last does not begin with C:
SELECT *
FROM employees
WHERE last_name NOT LIKE 'C%'

Matching a Pattern Anywhere Using the % Wildcard Twice


Use two instances of the % wildcard to match a particular pattern anywhere. This example
returns all records that contain a C anywhere in the last name:

SELECT *
FROM employees
WHERE last_name LIKE '%C%'

Finding a Pattern Match at a Specific Position


Use the _ wildcard to return data at a specific location. This example matches only if C occurs at
the third position of the last name column:

SELECT *
FROM employees
WHERE last_name LIKE '_ _C%'

Supported Wildcard Expressions in Transact SQL


There are several wildcard expressions supported by Transact SQL:

• The % wildcard matches zero or more characters of any type and can be used to define
wildcards both before and after the pattern. If you're familiar with DOS pattern matching,
it's the equivalent of the * wildcard in that syntax.
• The _ wildcard matches exactly one character of any type. It's the equivalent of the ?
wildcard in DOS pattern matching.
• Specify a list of characters by enclosing them in square brackets. For example, the
wildcard [aeiou] matches any vowel.
• Specify a range of characters by enclosing the range in square brackets. For example, the
wildcard [a-m] matches any letter in the first half of the alphabet.
• Negate a range of characters by including the carat character immediately inside of the
opening square bracket. For example, [^aeiou] matches any non-vowel character while
[^am] matches any character not in the first half of the alphabet.
Combining Wildcards for Complex Patterns
Combine these wildcards in complex patterns to perform more advanced queries. For example,
suppose you need to construct a list of all of your employees who have names that begin with a
letter from the first half of the alphabet but do not end with a vowel. You could use the following
query:
SELECT *
FROM employees
WHERE last_name LIKE '[a-m]%[^aeiou]'

Similarly, you could construct a list of all employees with last names consisting of exactly four
characters by using four instances of the _ pattern:
SELECT *
FROM employees
WHERE last_name LIKE '____'

As you can tell, the use of SQL pattern matching capabilities offers database users the ability to
go beyond simple text queries and perform advanced searching operations.

Conclusion :- simple SQL queries, string manipulation operations are performed successfully.

You might also like