Saturday 22 April 2017

use of concat() and lower() in sql

Generate the following two result sets:

1)Query an alphabetically ordered list of all names in OCCUPATIONS, immediately followed by the first letter of each profession as a parenthetical (i.e.: enclosed in parentheses). For example: AnActorName(A), ADoctorName(D), AProfessorName(P), and ASingerName(S).

2)Query the number of ocurrences of each occupation in OCCUPATIONS. Sort the occurrences in ascending order, and output them in the following format:

There are total [occupation_count] [occupation]s.

where [occupation_count] is the number of occurrences of an occupation in OCCUPATIONS and [occupation] is the lowercase occupation name. If more than one Occupation has the same [occupation_count], they should be ordered alphabetically.

the table is

occupations( name varchar(45), occupation varchar(25))
QUERY:

select concat(name,'(',left(occupation,1),')')
from occupations
order by name;
select concat('There are total ',count(occupation),' ',lower(occupation),'s','.')
from occupations
group by occupation

order by count(occupation),occupation;

output:
Aamina(D) 
Ashley(P) 
Belvet(P) 
Britney(P) 
Christeen(S) 
Eve(A) 
Jane(S) 
Jennifer(A) 
Jenny(S) 
Julia(D) 
Ketty(A) 
Kristeen(S) 
Maria(P) 
Meera(P) 
Naomi(P) 
Priya(D) 
Priyanka(P) 
Samantha(A) 
There are total 3 doctors. 
There are total 4 actors. 
There are total 4 singers. 

There are total 7 professors. 



Graph colouring using backtracking

GRAPH COLOURING:

Given a graph G (V,E) where V represents the vertices in the graph and E represents the edges in the graph . Given  m different colours to colour the nodes in such a way that no two adjacent nodes are of same colour.

eg:
for the given graph

and given two colours yellow and red

there are two solutions possible:






code:
#include<stdio.h>
int GAM[5][5];
int X[5];
int n,m;
int print()
{
    int i,j;
    printf("THE COLOURS OF EACH NODE IS:");
    printf("for node 1\t2\t3\t4\n");
    for(i=1;i<=n;i++)
    {

        printf("%d",X[i]);
        printf("\n");
    }

}
int issafe( int k,int p)
{
    int c;
    for(c=1;c<k;c++)
    {
        if(X[c]==p && GAM[c][k]==1)
            return 0;
    }
   return 1;
}
void graphcolor(int k,int m)
{
    int p;
    for(p=1;p<=m;p++)// p represents colour no
    {
        if(issafe(k,p))//checking if could be coloured or not
         {
            X[k]=p;
            if(k==n)
            print();
            else
                graphcolor(k+1,m);
         }
    }

}

int main()
{

    printf("enter the number of nodes");
    scanf("%d",&n);
    printf("enter 1 if edge exists else 0");
    int i,j;
    for( i=1;i<=n;i++)
        for(j=1;j<=n;j++)
    {
        printf("edge %d->%d",i,j);
        scanf("%d",&GAM[i][j]);

    }
    printf("enter number of colours available");
    scanf("%d",&m);
    graphcolor(1,m);
    return 0;
}

steps involved ;

for solution 1:(solution two similar to this)


Saturday 1 April 2017

Type of Triangle


Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the following statements for each record in the table:
  • Not A Triangle: The given values of AB, and C don't form a triangle.
  • Equilateral: It's a triangle with  sides of equal length.
  • Isosceles: It's a triangle with  sides of equal length.
  • Scalene: It's a triangle with  sides of differing lengths.
Input Format
The TRIANGLES table is described as follows:
Each row in the table denotes the lengths of each of a triangle's three sides.
Sample Input
Sample Output
Isosceles
Equilateral
Scalene
Not A Triangle
Explanation
Values in the tuple  form an Isosceles triangle, because 
Values in the tuple  form an Equilateral triangle, because . Values in the tuple  form a Scalene triangle, because 
Values in the tuple  cannot form a triangle because the combined value of sides  and  is not larger than that of side .

Query:
select
case
when (a+b)<=c or (a+c)<=b or (b+c)<=a  then 'Not A Triangle'
when a=b and b=c then 'Equilateral'
when (a=b and b<>c) or (b=c and a<>c) or (a=c and c<>b)  then 'Isosceles'
else    'Scalene'
end
from Triangles;

Marks Higher than 75

Query the Name of any student in STUDENTS who scored higher than  Marks. Order your output by the last three characters of each name. If two or more students both have names ending in the same last three characters (i.e.: Bobby, Robby, etc.), secondary sort them by ascending ID.
Input Format
The STUDENTS table is described as follows:The Name column only contains uppercase (A-Z) and lowercase (a-z) letters.
Sample Input
Sample Output
Ashley
Julia
Belvet
Explanation
Only Ashley, Julia, and Belvet have Marks > . If you look at the last three characters of each of their names, there are no duplicates and 'ley' < 'lia' < 'vet'.
Query:
MS SQL SERVER:
select Name
from students
where marks>75
order by right(Name,3),id ;

Weather Observation Station 7

Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your result cannot contain duplicates.
Input Format
The STATION table is described as follows:
where LAT_N is the northern latitude and LONG_W is the western longitude.
Query:
MYSQL:
select distinct city
from station
where city like '%a' or city like '%e' or city like '%i'or city like '%o' or city like '%u';
MS SQL SERVER:

select  distinct city
from station
where right(city,1) in('a','e','i','o','u');