I'm having some trouble figuring out a query. I have 2 tables, COMPANY, which contains a company id column, comp_id, and a name column. The second table, SOURCE, has a column for comp_id and a column for parts the company sells, parts.
How can I write a query to find the names of companies that produce all parts? I've figured that this uses not exist statements, but i can't figure out how.
Thanks.
Try this...
SELECT c.name from company join source s on (s.comp_id = c.comp_id) where parts = [:part_id];
Select name
from company, source
where company.comp_id=source.comp_id;
SELECT name, count(*) AS parts_count
FROM company, source
WHERE company.comp_id = source.comp_id
GROUP BY name
HAVING parts_count = (select count(*) from source)
Is very simple and command, suppose a part is "875"
SELECT name FROM company WHERE comp_id IN(SELECT comp_id FROM parts
WHERE parts = 875)
this allow you have the same part in distinct companies
Select name from companies,source where companies.comp_id=source.comp_id;
Related
I've made a table with the columns for a customer name and each activity they participate in. How can I can I count the activities for each name and display it?
I've done;
SELECT Activity_Name, COUNT(*) AS 'Number_of_activities'
FROM tablename
GROUP BY Activity_Name;
which gives me each a table of each activity and how many participants in each activity but not each customer and their number of activities
Apologies for anything I've done wrong, only a couple months into coding and first time posting on stack...
Considering I don't know how your schema looks exactly, this query should be a nice representation of the idea how to do it:
SELECT customer_name, COUNT(*) AS 'Number_of_activities_per_customer'
FROM tablename
GROUP BY customer_name;
I am trying to write a view to show name, and wrong_zipcode for the customer that has an incorrect zipcode in the customers table below, assume we have another table call usstates that has the correct zipcodes.
My code looks this way:
create view test as select name, zipcode from Customer c,usstates u where c.zipcode not in (select zipcode from usstates);
Customer table
usstates table
If I understand you question right, you got two tables. One with customer information and one with zip codes and you want to find all rows in the customer table where the zip code entered does not exist in the zip code table?
In that case you could use something like this.
SELECT NAME, ZIPCODE from Customers where not exists (SELECT TOP 1 ZIPCODE from MyZipCodeTable where Customers.ZIPCODE = MyZipCodeTable.ZIPCODE)
What do you think about that? We join the two tables with the zipcode and we eliminate the rows where the state doesn't match.
CREATE VIEW customers_with_wrong_zipcode AS
SELECT customers.customer_id, customers.zipcode AS wrong_zipcode, customers.state
FROM customers
INNER JOIN us_states ON us_states.zipcode = customers.zipcode
WHERE us_states.state <> customers.state;
You can use not exists
Select *
From customers c
Where not exists
(select 1 from usstates where zipcode = c.zipcode);
So I have a table with persons.
This table connects with vehicles with manyToMany.
I need to find how many times does id appear on the table person_vehicle.
How can I do this?
Thanks everybody for asking
Here are two suggestion.
For all the names:
Gives number of times the names are repeated.
SELECT count(name) FROM persons GROUP BY name;
Specifically for the jake:
SELECT count(name) FROM persons WHERE name = "Jake";
What have you tried? Depending on the table, you can do something like:
SELECT * FROM persons WHERE first_name = 'Jake'
Which will return the number of rows with the first name of "Jake"
Alternatively, if you only want one row with the total count of occurrences:
SELECT COUNT(first_name) FROM persons WHERE first_name = 'Jake'
You're looking for GROUP BY.
A table contains multiple columns. You have to decide first which column(s) of the data that you need are in common.
Suppose I have a table address(person_name, plot_no, pin);
If I try to select, for a particulate pin, how many people are living there:
SELECT pin, COUNT(*)
FROM address
GROUP BY pin;
If I try to select, for a particulate plot_no and pin, how many people are living there:
SELECT plot_no, pin, COUNT(*)
FROM address
GROUP BY plot_no, pin;
Use group by statement on the column, on which you are looking for duplicates if the count comes more than 1, it means they are repeating.
Select count(id), name from table_name group by id HAVING count(id) > 1;
Only the duplicates records will come.
Try this:
SELECT count(name) FROM persons WHERE name = "Jake";
I am trying to select a small number of records in a somewhat large database and run some queries on them.
I am incredibly new to programming so I am pretty well lost.
What I need to do is select all records where the Registraton# column equals a certain number, and then run the query on just those results.
I can put up what the db looks like and a more detailed explanation if needed, although I think it may be something simple that I am just missing.
Filtering records in a database is done with the WHERE clause.
Example, if you wanted to get all records from a Persons table, where the FirstName = 'David"
SELECT
FirstName,
LastName,
MiddleInitial,
BirthDate,
NumberOfChildren
FROM
Persons
WHERE
FirstName = 'David'
Your question indicates you've figured this much out, but are just missinbg the next piece.
If you need to query within the results of the above result set to only include people with more than two children, you'd just add to your WHERE clause using the AND keyword.
SELECT
FirstName,
LastName,
MiddleInitial,
BirthDate,
NumberOfChildren
FROM
Persons
WHERE
FirstName = 'David'
AND
NumberOfChildren > 3
Now, there ARE some situations where you really need to use a subquery. For example:
Assuming that each person has a PersonId and each person has a FatherId that corresponds to another person's PersonId...
PersonId FirstName LastName FatherId...
1 David Stratton 0
2 Matthew Stratton 1
Select FirstName,
LastName
FROM
Person
WHERE
FatherId IN (Select PersonId
From Person
WHERE FirstName = 'David')
Would return all of the children with a Father named David. (Using the sample data, Matthew would be returned.)
http://www.w3schools.com/sql/sql_where.asp
Would this be any use to you?
SELECT * from table_name WHERE Regestration# = number
I do not know what you have done up to now, but I imagine that you have a SQL query somewhere like
SELECT col1, col2, col3
FROM table
Append a where clause
SELECT col1, col2, col3
FROM table
WHERE "Registraton#" = number
See SO question SQL standard to escape column names?.
Try this:
SELECT *
FROM tableName
WHERE RegistrationNo = 'valueHere'
I am not certain about my solution. I would propose You to use view. You create view based on needed records. Then make needed queries and then you can delete the view.
View description: A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.
Example:
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
For more information: http://www.w3schools.com/sql/sql_view.asp
I have a small problem regarding a count after grouping some elements from a mysql table,
I have an orders table .. in which each order has several rows grouped by a code (named as codcomanda) ... I have to do a query which counts the number of orders per customer and lists only the name and number of orders.
This is what i came up (this might be dumb ... i'm not a pro programmer)
SELECT a.nume, a.tel, (
SELECT COUNT(*) AS `count`
FROM (
SELECT id AS `lwtemp`
FROM lw_comenzi_confirmate AS yt
WHERE status=1 AND yt.tel LIKE **a.tel**
GROUP BY yt.codcomanda
) AS b
) AS numar_comenzi
FROM lw_comenzi_confirmate AS a
WHERE status=1
GROUP BY tel;
nume = NAME
tel = PHONE (which is the distinct identifier for clients since there's no login system)
The problem with the above query is that I don't know how to match the a.tel with the one on which the first select is on. If I replace it with a number that is in the db it works....
Can anyone help me one how to refer to that var?
or maybe another solution on how to get this done?
If any more info is needed I`ll provide asap.
Please, correct me if I'm wrong in my understanding of your schema:
lw_comenzi_confirmate contains nume and tel of the customer;
lw_comenzi_confirmate contains order details (same table);
one order can have several entries in the lw_comenzi_confirmate table, order is distinguished by codcomanda field.
First, I highly recommend reading about Normalisation and fixing your database design.
The following should do the job for you:
SELECT nume, tel, count(DISTINCT codcomanda) AS cnt
FROM lw_comenzi_confirmate
WHERE status = 1
GROUP BY nume, tel
ORDER BY nume, tel;
You can test this query on SQL Fiddle.