본문 바로가기

카테고리 없음

국비 지원 학원 day9 java/데이터베이스(실습문제)/자바(2차배열,메소드)

package ex1108;

public class Ex02_변수있고리턴없고 {

   public static void main(String[] args) {

      // sumPrint 메소드 이용해서
      // 3과 5를 더한 출력하기

      sumPrint(3, 5);

   }

   public static void sumPrint(int a, int b) {

      System.out.print(a + b);

   }

}​
package ex1108;

public class Ex01_변수있고리턴있고 {

   public static void main(String[] args) {

      System.out.print(sum(2, 5));
      sum(2, 5);

   }

   public static int sum(int a, int b) {
      int result = a + b;

      return result;

   }

}
------------------------
--1) 서브쿼리
--서브쿼리 :쿼리 안에 쿼리가 있다.
--조회하고자 하는 값을 모르는 경우 활용
--종류 : 단일행, 다중행 반환쿼리
--단일행 : =, >, <, >=, <=, !=/, <>
--다중행 : in, any, all, <any, >all, exist

select avg(salary)
from employees;
--회사의 평균급여 이상 받는 직원의 이름, 급여를 조회..

SELECt last_name, salary
  FROM employees
  where salary > =(select avg(salary)
from employees);

--select 절에는 단일행만 쓸 수 있다.

-- 회사의 최고 급여를 받는 직원의 이름, 급여를 조회..

select last_name, salary
from employees
where salary>= (select max(salary)
                from employees);

--연습문제
--회사의 직원중 'IT_PROG' 직종의 평균 급여 보다 많이 받는 직원의
-- 이름,직종, 급여를 조회하시오..
select last_name, job_id,salary
from employees
where salary >=(SELECT avg(salary)
                        from employees
                        where job_id='IT_PROG');

-- Chen 과 같은 직종의 직원의 이름,직종, 급여를 조회하시오..
select last_name, job_id, salary
from employees
where job_id in (select job_id
                        from employees
                        where last_name ='Chen');

--3)영업부서(80)의 최소급여보다 많이 받는 부서의 
--  부서id, 부서이름, 최소급여를 조회.
-- join, group by, having....
select e.department_id, d.department_name, min(salary) as min_sal
from employees e , departments d
where e.department_id = d.department_id
GROup by  e.department_id, d.department_name
having min(salary) >(select min(salary)
                            from employees
                            where department_id = 80);
 
--다중행 연산자의 사용
--다중행 : in, <any, <all, >any, >all
--in (or) : 모든 행의 값에 대응하는 값
(9000, 6000, 4800, 4200);
--any(or) : <any 최대값보다 적은값(9000), >any 최소값보다 큰값(4200)
--all(and) : <all 최소값보다 적은값(4200), >all 최대값보다 큰값(9000)
select employee_id, last_name, job_id, salary
from employees
where salary > any(select salary
                    from employees
                    where department_id= 60);

select employee_id, last_name, job_id, salary
from employees
where salary > all(select salary
                    from employees
                    where department_id= 60);
                    
select employee_id,department_id, last_name, job_id, salary
from employees
where salary in(select salary
                    from employees
                    where department_id= 60);
--서브쿼리에 between의 사용
select employee_id,department_id, last_name, job_id, salary
from employees
where salary between(select min(salary)
                    from employees
                    where department_id= 60) and(select max(salary)
                    from employees
                    where department_id= 60);
                    
-- 실습

--회사 전체의 최대 급여,최소 급여, 급여 총합 및 평균 급여를 출력하시오
select  max(salary), min(salary),sum(salary), avg(salary)
    from employees;
    
--각 직업별, 최대 급여, 최소 급여, 급여 총합 및 평균 급여를 출력하시오.
--단 최대 급여는 max,최소 급여는 min,급여 총 합은 sum및
--평균 급여는 avg로 출력하고, 직업을 오름차순으로 정렬하시오

select job_id, max(salary), min(salary),sum(salary),avg(salary)
from employees
group by job_id
order by  job_id ;

--동일한 직종을 가진 사원들의 총 수를 출력하시오
select job_id, count(employee_id)
from employees
group by job_id;
---or
select job_id, count(*)
from employees
group by job_id;

select job_id, count(*),count(commission_pct)--null값에 있는 것은 카운트 안됨
from employees
group by job_id;
--매니저로 근무하는 사원들의 총 수를 출력하시오
select count(distinct manager_id)
from employees;

--사내의 최대 급여 및 최소 급여 차이를 출력하시오.
select max(salary)-min(salary) as cha
from employees;

--매니저의 사번 및 그 매니저 밑 사원들 중 최소 급여를 받는 사원의 급여를 출력하시오
--매니저가 없는 사람들은 제외한다.
-- 최소 급여가 5000 미만인 경우는 제외한다.
-- 급여 기준 역순으로 조회한다.
select manager_id, min(salary)
from employees
where manager_id is not null
group by manager_id
having min(salary) >= 5000
order by min(salary)desc;

--급여합계가 10,000보다 많은 직원의
--부서,직종별 급여합계, 평균급여, 직원 수 출력하세요
--급여 sun_sal는 3자리마다","형식으로,
--평균급 avg_sal는 소수점 이하 2자리 까지 출력하세요
select department_id,job_id, 
to_char(sum(salary),'99,999') sum_sal,
 to_char(avg(salary),'999,999.00'),
 count(*)
from employees
group by department_id,job_id;

--모든 사원들의 이름,부서 이름 및 부서 번호를 출력하시오.
select last_name, d.department_name, e.department_id
from employees e, departments d
where e.department_id = d.department_id ;

--커미션을 받는 모든 사람들의 이름,부서 명,지역 id 및 도시 명을 출력하시오
select e.last_name, d.department_name,
        l.location_id, l.city
        from employees e, departments d , locations l
        where e.department_id = d.department_id
        and d.location_id = l.location_id
        and e.commission_pct is not null;

--Zlotkey와 동일한 부서에 근무하는 모든 사원들의 사번 및 고용날짜를 출력하시오
    select employee_id,hire_date
from employees
    where department_id in (select department_id 
                                        from employees
                                        where last_name ='Zlotkey');
--회사 전체 평균 급여보다 더 급여를 많이 받는 사원들의 사번 및 이름을 출력하시오
 select employee_id, last_name
from employees
 where salary> (select avg(salary)
                from employees);
                
--이름에 u가 포함되는 사원들과 동일 부서에 근무하는 사원들의 사번 및 이름을 출력하시오.
select employee_id, last_name
from employees
where department_id in(select department_id
                                from employees
                                where last_name like'%u%');

--이름이 Davies 인 사람보다 후에 고용된 사원들의 이름 및  고용일자를 출력하시오.
--고용일자를 역순으로 출력하시오
select last_name, hire_date
from employees
where hire_date > all (select hire_date
                            from employees
                            where last_name ='Davies');

--King 을 매니저로 두고 있는 모든 사원들의 이름 및 급여를 출력하시오
select last_name, salary, manager_id
from employees
 where manager_id in(select employee_id
                    from employees
                    where last_name ='King');

오늘은 실전 문제를 계속 풀었는데 3분에 1정도 풀었는것 같다 특히 조인과 다중문,에서 많이 모자란다

틈틈히 문제를 더 풀어야함

 

   public static void main(String[] args) {
      // 입력부
            int[][] array = new int[5][5];
            int c = 1;
            for (int i = 0; i < array.length; i++) {
               for (int a = 0; a < array[i].length; a++) {
                  array[i][a] = c++;
               }
               
            }
      // 출력부
            
            for (int i = 0; i < array.length; i++) {
               for (int a = 0; a < array[i].length; a++) {
                  System.out.print(array[i][4-a] +"\t");
               
               }
               System.out.println();
      int cnt = 0;

      for (int i = 0; i < array.length; i++) {
         for (int k = 0; k < array[i].length; k++) {
            array[i][k] = ++cnt;
         }
      }

      for (int i = 0; i < array.length; i++) {
         for (int k = 0; k < array[i].length; k++) {
            if(i%2 == 1) {
               System.out.print(array[i][4-k] +"\t");
            }else System.out.print(array[i][k] +"\t");
         }System.out.println();
      }

      int cnt = 0;

      for (int i = 0; i < array.length; i++) {
         for (int k = 0; k < array[i].length; k++) {
            array[i][k] = ++cnt;
         }
      }

      for (int i = 0; i < array.length; i++) {
         for (int k = 0; k < array[i].length; k++) {
            if(i%2 == 1) {
               System.out.print(array[i][4-k] +"\t");
            }else System.out.print(array[i][k] +"\t");
         }System.out.println();
      }

의외로 다들 못풀어서 놀랬다 남는시간에 자바 페스티벌 문제 푸는 여유가 생김

package ex1108;

public class Ex01_변수있고리턴있고 {

   public static void main(String[] args) {

      System.out.print(sum(2, 5));
      sum(2, 5);

   }

   public static int sum(int a, int b) {
      int result = a + b;

      return result;

   }

}

변수와 리턴 값이 있는 vers

package ex1108;

public class Ex02_변수있고리턴없고 {

   public static void main(String[] args) {

      // sumPrint 메소드 이용해서
      // 3과 5를 더한 출력하기

      sumPrint(3, 5);

   }

   public static void sumPrint(int a, int b) {

      System.out.print(a + b);

   }

}

변수있고 리턴 값이없는 vers

Scanner sc = new Scanner(System.in);

      System.out.print("첫 번째 정수 입력 >> ");
      int num1 = sc.nextInt();
      
      System.out.print("두 번째 정수 입력 >> ");
      int num2 = sc.nextInt();
      
      System.out.print("연산자(+,-,*,/) 입력 >> ");
      String op = sc.next();
      
      
      System.out.println(cal(num1, num2, op));
      
      
   }
   public static int cal(int a,int b, String c) {
      int result =0;
      if(c.equals("+")) {
        result = a+b;
      }else if(c.equals("-")) {
         result = a-b;
      }else if(c.equals("*")) {
         result = a*b;
      }else if(c.equals("/")) {
         result = a/b;
      }
      
      return result;
   }

아직 까지는 무난함  아 참고로 하나의 결과에는 하나의 리턴 값이 여야함

그후 보충시간에 form 9시까지 하고 헬스장 감

그 중에 기억남는거

정답은 1,2, 4, 5,8,3,2